使用触发器绑定 WPF Datagrid 单元格背景颜色 [英] Binding WPF Datagrid cell background colour with trigger

查看:141
本文介绍了使用触发器绑定 WPF Datagrid 单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 WPF 数据网格单元格的背景颜色在内容被修改时改变颜色.每个单元格背后都有一个 ViewModel 对象,该对象包含以下属性 - Value、OriginalValue 和 Modified.当用户编辑单元格内容时,这会通过数据绑定自动触发 Amount 属性.然后,此属性设置器会根据原始值对其进行检查,并将布尔值 Modified 属性分别设置为 true 或 false,通知这些属性的绑定进行更新.

I want the background colour of a WPF datagrid cell to change colour when the contents have been modified. Each cell has behind it a ViewModel object which contains the following properties - Value, OriginalValue and Modified. When the user edits the cell contents, this automatically triggers the Amount property via data binding. This property setter then checks it against the original value and sets the boolean Modified property to true or false respectively, notifies the bindings for those properties to update.

到目前为止,我已经通过 DataGridTextColumn 的 ElementStyle 属性上的样式实现了部分结果,如下所示

I have so far achieved a partial result with a Style on the ElementStyle property of the DataGridTextColumn as follows

<Style x:Key="DataGridTextStyle" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=MyViewModel.Modified}" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

这会更新文本内容的背景颜色,但这只是单元格中心的一小块区域.我希望整个单元格更新它的背景颜色,而不仅仅是 textblock 属性.

This updates the text content background colour, but that is only a small area in the center of the cell. I want the entire cell to updates it's background colour, not just the textblock attribute.

我可以修改上面的触发器在可视化树中向上搜索找到一个父DataGridCell并在其上设置Background属性,而不是只设置当前文本块的背景颜色吗?

Can I modify the above trigger to search upwards in the visual tree to find a parent DataGridCell and set the Background property on that, rather than setting the background colour of the current textblock only?

推荐答案

你需要设置 CellStyle 来定位 DataGridCell 而不是 TextBlock.

You need to set CellStyle to target DataGridCell instead of only TextBlock.

如果您希望将此 dataTrigger 应用于数据网格中的所有单元格,请在 DataGrid CellStyle 上设置样式,否则您也可以在特定的 DataGridTextColumn CellStyle 上执行此操作.

If you want this dataTrigger to be applied for all cells in your dataGrid, set style on DataGrid CellStyle otherwise you can do that on specific DataGridTextColumn CellStyle as well.

数据网格

     <DataGrid>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MyViewModel.Modified}"
                                 Value="True">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

DataGridTextColumn

     <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding MyViewModel.Modified}" 
                                         Value="True">
                                <Setter Property="Background" Value="Yellow"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

这篇关于使用触发器绑定 WPF Datagrid 单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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