如何根据代码中列出的值更改XAML中的数据网格行颜色? [英] How do I change a datagrid row color in XAML based on the value listed in code?

查看:63
本文介绍了如何根据代码中列出的值更改XAML中的数据网格行颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有VB.NET代码中提供的值列表.如果四个值中的每一个均为0,我想将XAML行更改为灰色背景色.我尝试使用mulitdatatrigger,但是没有用.有什么想法吗?

I have a list of values provided in VB.NET code that. If each of four values are 0, I want to change the XAML row to a gray background color. I tried using a mulitdatatrigger, but it did not work. Any ideas?

谢谢.

克里斯

克里斯·塞尔金特(Chris Sergent)

Chris Sergent

推荐答案

您的意思是,如果全部4个都是0,那么灰色?

You mean if all 4 are 0 then gray?

这对我有用:

        <DataGrid ItemsSource="{Binding Rows}"
                  AutoGenerateColumns="False"
                  >
            <DataGrid.Resources>
                <Style TargetType="DataGridCell">
                      <Setter Property="Background" Value="Transparent"/>
                </Style>
            </DataGrid.Resources>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Col1}" Value="0"/>
                                <Condition Binding="{Binding Col2}" Value="0"/>
                                <Condition Binding="{Binding Col3}" Value="0"/>
                                <Condition Binding="{Binding Col4}" Value="0"/>
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Background" Value="Gray"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Col1}" Header="Col1"/>
                <DataGridTextColumn Binding="{Binding Col2}" Header="Col1"/>
                <DataGridTextColumn Binding="{Binding Col3}" Header="Col1"/>
                <DataGridTextColumn Binding="{Binding Col4}" Header="Col1"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid

我的视图模型是c#,但这确实无关紧要:

My viewmodel is c#, but that's irrelevant really:

    public class MainWindowViewModel
    {
        public ObservableCollection<RowVM> Rows { get; set; }
        = new ObservableCollection<RowVM>
        {
            new RowVM {Col1=2, Col2=3, Col3=4, Col4=0},
            new RowVM {Col1=0, Col2=3, Col3=4, Col4=0},
            new RowVM {Col1=0, Col2=0, Col3=0, Col4=0},
            new RowVM {Col1=2, Col2=0, Col3=4, Col4=0},
            new RowVM {Col1=0, Col2=3, Col3=0, Col4=0}
        };
    }

外观如下:


这篇关于如何根据代码中列出的值更改XAML中的数据网格行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆