Associate a Datagrid Cell Background Color with a Trigger

I want the background color of a WPF datagrid cell to change color when the content has been changed. Each cell has a ViewModel object that contains the following properties: Value, OriginalValue, and Modified. When the user edits the contents of the cell, this automatically calls the Amount property by means of data binding. Then this property installer checks it for the initial value and sets the boolean Modified property to true or false, respectively, notifies the bindings for these properties for updating.

I have so far achieved a partial result with a style in the ElementStyle property for a 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 background color of the text content, but it is only a small area in the center of the cell. I want the whole cell to update its background color, and not just the textblock attribute.

Can I change the trigger above to search up in the visual tree to find the parent DataGridCell and set the Background property for it, and not set only the background color of the current text block?

+8
c # css wpf datagrid
source share
2 answers

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

If you want this dataTrigger to be applied to all cells of your DataGrid, set the style to DataGrid CellStyle , otherwise you can do this on a specific 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> 
+17
source share

Others May benefit from this WPF "Dynamic Data Triggers" in code outside the method

This code allows users to highlight data rows with the text they need.

  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"); 
  • Parm CurrentTextToFilter is the text entered by the user in the XAML Textbox.Text property, which was bound to the code behind.

  • The XDG variable is the XAML name with the datagrid data, and RowStyle is set to a new style.

  • Be sure to add the installer to the DataTrigger as shown. If you add it directly to Rowstyle, all lines turn red.
+4
source share

All Articles