【问题标题】:Datagrid cell color change using converter使用转换器的 Datagrid 单元格颜色更改
【发布时间】:2017-02-06 02:48:57
【问题描述】:

我正在尝试通过使用将单元格作为参数的转换器来更改数据网格单元格的颜色,因为我需要单元格和行来选择单元格的颜色。数据是动态的,因此我没有任何模型。问题是转换器在加载数据时没有受到影响。

private void RDataGrid_AutoGeneratedColumns(object sender, EventArgs e)
    {
        foreach (var dataGridColumn in RDataGrid.Columns)
        {
            var textColumn = dataGridColumn as DataGridTextColumn;
            if (textColumn == null) continue;

            textColumn.ElementStyle = FindResource("gridElementStyle") as Style;
            textColumn.EditingElementStyle = FindResource("gridEditElementStyle") as Style;
            textColumn.CellStyle = FindResource("gridCellStyle") as Style;

        }
    }

这是绑定到数据网格的样式。

 <Style x:Key="gridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background"
                         Value="{Binding .
                         , RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}
                         ,Converter={StaticResource ColorConverter}   
                            }"  />
</Style>

转换器:

 public class ColorConverter : IValueConverter
{
     //private string[,] yourarray = new string[100, 100];
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DataGridCell cell = (DataGridCell)value;
        return Brushes.Red;
        //int x = cell.Column.DisplayIndex;
        //var parent = VisualTreeHelper.GetParent(cell);
        //while (parent != null && parent.GetType() != typeof(DataGridRow))
        //{
        //    parent = VisualTreeHelper.GetParent(parent);
        //}
        //int y = ((DataGridRow)parent).GetIndex();

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

【问题讨论】:

    标签: c# wpf datagrid converter


    【解决方案1】:

    这对我有用:

    <Style TargetType="DataGridCell">
                                    <Setter Property="Foreground" Value="Black"/>
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <MultiBinding Converter="{StaticResource NameToBrushMultiValueConverter}" >
                                                <MultiBinding.Bindings>
                                                    <Binding RelativeSource="{RelativeSource AncestorType=Window}" Path="DataContext"/>
                                                    <Binding RelativeSource="{RelativeSource AncestorType=DataGrid}"></Binding>
                                                    <Binding RelativeSource="{RelativeSource Self}"/>
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
    

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2011-07-29
      • 2023-03-28
      • 2023-03-08
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多