如何根据 DataGrid 中的值以编程方式(WPF 和 C#)设置 WPF DataGrid 的行背景颜色

发布于 2024-09-14 07:26:52 字数 355 浏览 5 评论 0原文

我有一个与接口对象列表绑定的 WPF Datagrid。 考虑一下,ClsEmployee 类实现了 I_Employee 接口,其中包含 Empl_Id、Empl_Name、Department、Address 和 City_name 属性。

列表_emplList;

考虑一下,_emplList 有 10 个项目。

dgEmployeeGrid.ItemsSource = _emplList;

问题: 现在,如果用户单击按钮,那么我应该能够读取 City_name。基于 City_name,我应该能够通过 C# 代码动态设置行的颜色(每行的颜色可以不同)。

请帮助我该怎么做?

提前致谢!

I have a WPF Datagrid binded with list of interface objects.
Consider, ClsEmployee class implements I_Employee interface with properties Empl_Id, Empl_Name, Department, Address and City_name.

List _emplList;

consider, _emplList has 10 items.

dgEmployeeGrid.ItemsSource = _emplList;

Question:
Now, if the user clicks on a button, then i should be able to read the City_name. Based on the City_name, i should be able to set the color (Color can be different for each row) for the rows dynamically through C# code.

Please help me how to do this?

Thanks in advance!

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

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

发布评论

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

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-09-21 07:26:52

构建一个 ValueConverter ,它接受“网格中适当类型的值”,然后使用 ValueConverter 将行的背景颜色绑定到该字段,以提供 颜色画笔 或您想要放入其中的任何其他类型的画笔(有意义)。

编辑

这是一个将布尔颜色转换为画笔颜色的转换器。这个类有两个属性,称为“True”和“False”,我们设置一个 Brush,当布尔值与属性匹配时将使用它们。转换器是一种方法,不会将画笔转换回布尔值。

用于创建转换器实例并设置属性的 XAML

<cga:BoolToBrushConverter x:Key="trueIsGreen" 
    True="Green" 
    False="Red"/>

C# 转换器代码

[ValueConversion(typeof(bool), typeof(Brush))]
public class BoolToBrushConverter : IValueConverter
{
    public Brush True
    {
        get; set;
    }

    public Brush False
    {
        get; set;
    }

    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        if (targetType != typeof(Brush))
        {
            return null;
        }

        return ((bool) value) ? True : False;
    }

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

绑定值并转换为采用画笔的对象上的字段的示例

<Ellipse Width="10" Height="10" 
       Fill="{Binding Path=Reviewer.IsMentor, Mode=OneWay, 
                      Converter={StaticResource trueIsGreen}}"/>

I假设您熟悉 WPF 中的数据绑定,并且不会详细说明解决方案的这方面,但是当 Reviewer.IsMentor 为 true 时,转换器将提供“绿色”画笔(当转换器创建)到椭圆的 Fill 属性。

Build a ValueConverter that takes in a "value" of the appropriate type in the grid, and then bind the Background color of the row to that field with the ValueConverter in there to provide the Color brush or whatever other kind of brush (that makes sense) that you'd like to put in there.

EDIT

Here is a Converter that converts a bool to a Brush color. This class has two properties called "True" and "False" which we set a Brush to that will be used when the boolean value matches the property. The converter is one way and does not convert brushes back to boolean values.

XAML to create instance of the Converter and set properties

<cga:BoolToBrushConverter x:Key="trueIsGreen" 
    True="Green" 
    False="Red"/>

C# Converter Code

[ValueConversion(typeof(bool), typeof(Brush))]
public class BoolToBrushConverter : IValueConverter
{
    public Brush True
    {
        get; set;
    }

    public Brush False
    {
        get; set;
    }

    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        if (targetType != typeof(Brush))
        {
            return null;
        }

        return ((bool) value) ? True : False;
    }

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

Example of binding value and convert to a field on an object that takes brushes

<Ellipse Width="10" Height="10" 
       Fill="{Binding Path=Reviewer.IsMentor, Mode=OneWay, 
                      Converter={StaticResource trueIsGreen}}"/>

I'm assuming you are familiar with databinding in WPF and won't elaborate that aspect of the solution but when the Reviewer.IsMentor is true, the Converter will provide a "Green" brush (sent when the Converter was created) to the Fill property of the ellipse.

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