如何根据列值设置 WPF DataGrid 行的一部分的背景颜色?

发布于 10-30 10:53 字数 362 浏览 4 评论 0原文

假设我有一个包含 5 列的 DataGrid,从客户类集合加载(使用 AutoGenerateColumns=True)。根据第一、第三和第四列中的值,我需要使用以下选项之一设置行的背景:

col num:    1    2    3    4    5
 Option1: gray gray gray gray gray
 Option2: gray red  red  gray gray
 Option3: gray gray gray red  red
 Option4: gray blue blue blue blue

这需要为每一行单独完成。

我见过一些为行背景设置转换器的示例,但是如何设置单个单元格背景?

Say I have a DataGrid with 5 columns, loaded (using AutoGenerateColumns=True) from a custome class collection. Based on the value in the first, third and fourth columns, I need to set the background of the row with one of the following options:

col num:    1    2    3    4    5
 Option1: gray gray gray gray gray
 Option2: gray red  red  gray gray
 Option3: gray gray gray red  red
 Option4: gray blue blue blue blue

This needs to be done for each row separately.

I've seen some examples of setting a converter for the row background, but how can I set individual cell backgrounds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你而言2024-11-06 10:53:33

您可能需要使用自定义列,然后您可以将整行传递到转换器中,同时使用 ConverterParameter 指定当前列,这样您就可以比较所有值:

<DataGridTextColumn Binding="{Binding Column1}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="{Binding Converter={StaticResource ValueToBrushConverter},
                                    ConverterParameter=1}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column2}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="{Binding Converter={StaticResource ValueToBrushConverter},
                                    ConverterParameter=2}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<!-- ... -->

我不完全是那个表你的,但你可能可以自己弄清楚如何在转换器中返回正确的值。


转换器骨架可能是这样的:

public class ValueToBrushConverter : IValueConverter
{
    /// <summary>
    /// 1st Indexer: Columns
    /// 2nd Indexer: Options
    /// </summary>
    Brush[,] _brushMatrix = new Brush[5,4]
    {
        { Brushes.Gray, Brushes.Gray, Brushes.Gray, Brushes.Gray },
        { Brushes.Gray, Brushes.Red, Brushes.Gray, Brushes.Blue },
        { Brushes.Gray, Brushes.Red, Brushes.Gray, Brushes.Blue },
        { Brushes.Gray, Brushes.Gray, Brushes.Red, Brushes.Blue },
        { Brushes.Gray, Brushes.Gray, Brushes.Red, Brushes.Blue }
    };

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        RowDataItem item = value as RowDataItem;
        int currentColumn = int.Parse(parameter as string);
        int currentRowOption;

        #region Internal logic here, e.g
        if (item.Col1 == "I" && item.Col3 == "Love" && item.Col2 == "Converters")
        {
            currentRowOption = 1;
        }
        //...
        #endregion

        return _brushMatrix[currentColumn, currentRowOption];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

You probably need to use custom columns, then you can pass the whole row into a converter while using the ConverterParameter to specify the current column, that way you can compare all the values:

<DataGridTextColumn Binding="{Binding Column1}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="{Binding Converter={StaticResource ValueToBrushConverter},
                                    ConverterParameter=1}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Column2}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background"
                    Value="{Binding Converter={StaticResource ValueToBrushConverter},
                                    ConverterParameter=2}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<!-- ... -->

I do not quite that table of yours but you probably can figure out how to return the correct value in the converter on your own.


Converter skeleton could be something like this:

public class ValueToBrushConverter : IValueConverter
{
    /// <summary>
    /// 1st Indexer: Columns
    /// 2nd Indexer: Options
    /// </summary>
    Brush[,] _brushMatrix = new Brush[5,4]
    {
        { Brushes.Gray, Brushes.Gray, Brushes.Gray, Brushes.Gray },
        { Brushes.Gray, Brushes.Red, Brushes.Gray, Brushes.Blue },
        { Brushes.Gray, Brushes.Red, Brushes.Gray, Brushes.Blue },
        { Brushes.Gray, Brushes.Gray, Brushes.Red, Brushes.Blue },
        { Brushes.Gray, Brushes.Gray, Brushes.Red, Brushes.Blue }
    };

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        RowDataItem item = value as RowDataItem;
        int currentColumn = int.Parse(parameter as string);
        int currentRowOption;

        #region Internal logic here, e.g
        if (item.Col1 == "I" && item.Col3 == "Love" && item.Col2 == "Converters")
        {
            currentRowOption = 1;
        }
        //...
        #endregion

        return _brushMatrix[currentColumn, currentRowOption];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
帅的被狗咬2024-11-06 10:53:33

如果您想要后端代码解决方案,我确信您可以获取 LoadingRow 事件来检查单个行的数据上下文,并以这种方式找到生成的单元格。不过我以前没这样做过!

If you want a back end code solution, I'm sure you could get the LoadingRow event to check the individual row's datacontext, and find the generated cell that way as well. I haven't done that before though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文