简体   繁体   English

WPF:当GridView中的两个特定单元格的值不同时,更改GridView单元格颜色

[英]WPF: Change GridView cell colors when two specific cells in the GridView differ in values

I have the following problem: I have a GridView embedded into a ListView . 我有以下问题:我有一个嵌入到ListViewGridView It has two columns (the first and second ones) which initially have the same values in their cells, but by using other UI elements the user may able to change the value in a cell in the second column. 它有两列(第一列和第二列),它们的单元格中最初具有相同的值,但通过使用其他UI元素,用户可以更改第二列中单元格中的值。 What I want to do is to change the color of cells in the same row , when their values differ due to a change from the UI . 我想要做的是改变同一row中单元格的颜色,当它们的值因UI的变化而不同时。 Here is the ListView and GridView I am talking about: 这是我正在讨论的ListViewGridView

在此输入图像描述

Here, the first cells in both "Observation" and "Hidden State" columns should change their colors, indicating a difference in the values. 此处,“观察”和“隐藏状态”列中的第一个单元格应更改其颜色,指示值的差异。 And I have the following XAML code for the List and GridViews : 我有List和GridViews的以下XAML代码:

    <ListView Grid.Row="6" Margin="10" Name="ObservationsListView" SelectionChanged="ObservationsListView_SelectionChanged_1">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Observation" Width="122" DisplayMemberBinding="{Binding observationStr}" />
                <GridViewColumn Header="Hidden State" Width="122" DisplayMemberBinding="{Binding stateStr}" />
                <GridViewColumn Header="Value" Width="122" DisplayMemberBinding="{Binding valueStr}" />
            </GridView>
        </ListView.View>
    </ListView>

What is the easiest way to do that? 最简单的方法是什么? Simply, something like 简单地说,像

 GridView.Columns[0].Cell[currentCellIndex].Color = RED
 GridView.Columns[1].Cell[currentCellIndex].Color = RED

would do fine for me, but I am aware that things are more complex in WPF due to all the data binding stuff. 对我来说会很好,但我知道由于所有数据绑定的东西, WPF中的事情更复杂。 A short way solution like the above one would be great for my causes here. 像上面这样的简短解决方案对我的原因很有帮助。

The easiest thing that you can do is to add another property to your class that has the observationStr and stateStr properties in it. 你可以做最简单的事情是另一个属性添加到您的类具有observationStrstateStr在它的属性。 The new property should be of type bool and return true when the values do not equal each other and false when they do. 新属性应为bool类型,并且当值彼此相等时返回true ,否则返回false Then you can data bind to that property in a Trigger in the grid to update the colour for you: 然后,您可以在网格中的Trigger中绑定到该属性的数据,以便为您更新颜色:

public bool AreValuesChanged
{
    get { return observationStr != stateStr; }
}

Of course, you'd also need to call the NotifyPropertyChanged("AreValuesChanged") method when you update the other two values to make this work: 当然,当您更新其他两个值以使其工作时,您还需要调用NotifyPropertyChanged("AreValuesChanged")方法:

public string observationStr // repeat for stateStr
{
    get { return _observationStr; }
    set { _observationStr = value; NotifyPropertyChanged(observationStr); 
        NotifyPropertyChanged(AreValuesChanged); }
}

For the colour change, you could add a simple DataTrigger into a Style - make sure that this is declared within scope of the ListView control: 对于颜色更改,您可以将一个简单的DataTrigger添加到Style - 确保在ListView控件的范围内声明它:

<Style TargetType="{x:Type ListViewItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding AreValuesChanged}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM