WPF - 如何从DataGridRow获取单元格?

maa*_*yaa 10 wpf datagrid datagridcell

我有一个数据绑定的DataGrid与交替的行背景颜色.我想根据它包含的数据对单元格进行不同的着色.我已经尝试过这个线程建议的解决方案

http://wpf.codeplex.com/Thread/View.aspx?ThreadId=51143

但,

DataGridCellsPresenter presenter = GetVisualChild(row)

始终返回null.

我在用

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
Run Code Online (Sandbox Code Playgroud)

但DataGridRow的VisualTreeHelper.GetChildrenCount()始终返回0.我已经验证DataGridRow不为null并且已经填充了数据.任何帮助表示赞赏.

谢谢.

den*_*zov 13

如果您知道要访问的单元格的行和索引,那么您可以在代码中执行以下操作:

//here's usage
var cell = myDataGrid.GetCell(row, columnIndex);
if(cell != null)
    cell.Background = Brushes.Green;
Run Code Online (Sandbox Code Playgroud)

DataGrid扩展:

public static class DataGridExtensions
{
    public static DataGridCell GetCell(this DataGrid grid,  DataGridRow row, int columnIndex = 0)
    {
        if (row == null) return null;

        var presenter = row.FindVisualChild<DataGridCellsPresenter>();
        if (presenter == null) return null;

        var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        if (cell != null) return cell;

        // now try to bring into view and retreive the cell
        grid.ScrollIntoView(row, grid.Columns[columnIndex]);
        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);

        return cell;
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您在答案中添加以下[方法](http://stackoverflow.com/a/25229554/2470362),那就太好了. (3认同)

Chr*_*mes 8

首先,不要在代码隐藏中执行此操作.你用这种做事方式来对抗框架.WPF的设计不同; 你必须考虑框架如何要你做事.在WPF的情况下,它是XAML标记+转换器类.

你需要两件事来实现你想要的东西:

  • 适当的XAML标记来设置DataGrid的样式
  • 一个IValueConverter实现,用于将文本的值转换为正确的高亮颜色.

开始:

XAML在您的Datagrid中

您要做的第一件事是定义为​​DataGrid单元格设置样式所需的XAML.它看起来像这样:

<toolkit:DataGrid.CellStyle>
      <Style TargetType="{x:Type toolkit:DataGridCell}">
        <Style.Setters>
          <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource dataGridCellConverter}}" />
        </Style.Setters>
      </Style>
    </toolkit:DataGrid.CellStyle>
Run Code Online (Sandbox Code Playgroud)

这样做是设置绑定到RelativeSource(DataGridCell)并告诉它使用单元格的Content.Text作为传递给Converter(dataGridCellConverter)的值.

的IValueConverter

接下来您需要的是IValueConverter实现,以根据单元格的文本实际确定颜色:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace UserControls.Utility.Converters
{
  public class DataGridCellConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      if (value == null) return Colors.White.ToString();

      if (value.ToString().ToUpper().Contains("CMS")) return "LIME";

      return "ORANGE";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我只是寻找文本"CMS"并着色背景单元格; 如果"CMS"不存在,则返回橙色.

指定资源

现在,您需要在window/usercontrol中添加标记,以将转换器指定为适当的资源:

<UserControl.Resources>
    <Converters:DataGridCellConverter x:Key="dataGridCellConverter"/>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

这应该做到!祝好运.

  • '在WPF的情况下,它是XAML标记+转换器类. - 这是你个人的看法 (3认同)