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

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

问题描述

我要当内容被修改一个WPF DataGrid单元格的背景颜色来改变颜色。每个单元有它背后包含以下属性视图模型对象 - 价值OriginalValue和修改。当用户编辑单元格内容,这会自动触发通过数据绑定金额属性。此属性setter然后检查它与原始值分别设置布尔修改属性为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>

这更新文本内容的背景颜色,但是只有在小区的中心的小区域。我希望整个小区以更新它的背景色,而不仅仅是文字块的属性。

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和背景属性设置上,而不是设置当前文本块的背景颜色而已?

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>
        <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天全站免登陆