2010-09-08 131 views
9

我有一个数据绑定DataGrid交替行背景颜色。我想根据包含的数据对单元格进行不同的着色。我已经被这个线程WPF - 如何从DataGridRow获取单元格?

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

但是,

DataGridCellsPresenter主持人= GetVisualChild(行)

总是返回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; 
    } 

但VisualTreeHelper.GetChildrenCount(一DataGridRow的)总是返回0。我已验证DataGridRow不为空,并已被填充了数据。任何帮助表示赞赏。

谢谢。

回答

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> 

这是什么东西做的是建立一个绑定到的RelativeSource(在DataGridCell),并告诉它使用的电池的Content.Text作为值传递给转换器(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(); 
    } 
    } 
} 

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

指定资源

现在,你需要在你的窗口/用户控件添加标记来指定转换器作为一个合适的资源:

<UserControl.Resources> 
    <Converters:DataGridCellConverter x:Key="dataGridCellConverter"/> 
</UserControl.Resources> 

而且应该这样做!祝你好运。

+0

+1 - 非常详细的答案 – David 2011-03-11 10:45:45

+3

'在WPF的情况下,它是XAML标记+转换器类。' - 这是非常你的个人看法 – 2014-03-02 13:51:34

11

如果你知道你行,你想接入小区的指数,那么这里就是你可以在代码中做到这一点:

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

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; 
    } 
+1

这就是真正的答案。 – 2014-03-02 13:50:54

+0

我想这样的事情,但它似乎并没有工作。你可以检查这个主题http://stackoverflow.com/questions/32584353/wpf-control-datagrid-cell-color-change – 2015-09-18 07:09:16

+1

它很好,如果你添加以下[方法](http://stackoverflow.com/a/25229554/2470362)的答案。 – 2015-09-29 15:21:30

相关问题