C# 使用触发器绑定 WPF Datagrid 单元格背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19841649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding WPF Datagrid cell background colour with trigger
提问by NZJames
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.
我希望 WPF 数据网格单元格的背景颜色在内容被修改后改变颜色。每个单元格背后都有一个 ViewModel 对象,其中包含以下属性 - Value、OriginalValue 和 Modified。当用户编辑单元格内容时,这会通过数据绑定自动触发 Amount 属性。然后,此属性设置器会根据原始值对其进行检查,并将布尔值 Modified 属性分别设置为 true 或 false,通知这些属性的绑定进行更新。
I have so far achieved a partial result with a Style on the ElementStyle property of the DataGridTextColumn as follows
到目前为止,我已经通过 DataGridTextColumn 的 ElementStyle 属性上的样式实现了部分结果,如下所示
<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.
这会更新文本内容的背景颜色,但这只是单元格中心的一小块区域。我希望整个单元格更新它的背景颜色,而不仅仅是 textblock 属性。
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?
我可以修改上面的触发器以在可视化树中向上搜索以找到父 DataGridCell 并在其上设置 Background 属性,而不是仅设置当前文本块的背景颜色吗?
采纳答案by Rohit Vats
You need to set CellStyle
to target DataGridCell
instead of only TextBlock
.
您需要设置CellStyle
为 targetDataGridCell
而不是 only TextBlock
。
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.
如果您希望将此 dataTrigger 应用于 dataGrid 中的所有单元格,请将样式设置为 onDataGrid CellStyle
否则您也可以在特定的情况下执行此操作DataGridTextColumn CellStyle
。
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>
回答by John Peters
Others May benefit from this WPF "Dynamic Data Triggers" in code behind method
其他人可能会从这个 WPF“动态数据触发器”中的代码隐藏方法中受益
This code allows users to highlight data rows with the specified text they want.
此代码允许用户使用他们想要的指定文本突出显示数据行。
var st = new Style();
st.TargetType = typeof(DataGridRow);
var RedSetter = new Setter( DataGridRow.BackgroundProperty, Brushes.Red);
var dt = new DataTrigger(){
Value = CurrentTextToFilter,
Binding = new Binding("Value")
};
dt.Setters.Add(RedSetter);
st.Triggers.Add(dt);
XDG.RowStyle = st;
PropChanged("MainDataCollection");
The parm CurrentTextToFilter the text the user entered into a XAML Textbox.Text property that was bound to the code behind.
The variable XDG is the datagrid XAML name and the RowStyle is set to the new style.
- Make sure to add the setter to the DataTrigger as shown. If you add it directly to the Rowstyle, all the rows will turn red.
参数 CurrentTextToFilter 用户输入到绑定到后面代码的 XAML Textbox.Text 属性中的文本。
变量 XDG 是数据网格 XAML 名称,RowStyle 设置为新样式。
- 确保将 setter 添加到 DataTrigger,如图所示。如果直接将其添加到 Rowstyle 中,则所有行都会变为红色。