Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding WPF Datagrid cell background colour with trigger

Tags:

c#

css

wpf

datagrid

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.

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.

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?

like image 297
NZJames Avatar asked Nov 07 '13 16:11

NZJames


1 Answers

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

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.
like image 167
JWP Avatar answered Sep 20 '22 14:09

JWP