wpfxamldatagridtriggersmultitrigger

WPF XAML How to write a trigger or multitrigger for datagrid?


1 if you select the cell, set row background on a white colour

and

2 if you select the cell and row background is yellow, does not change the row background

I write this code for first condition, but I don't know how to make for first and second conditions together. Maybe I need a MultiTrigger ? :

                                <DataGrid.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                                        <Style TargetType="DataGridCell">
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected"
                                                            Value="True">
                                                    <Setter Property="Background"
                                                            Value="White" />
                                                    <Setter Property="Foreground"
                                                            Value="Black" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>

                                </DataGrid.Resources>

Solution

  • I guess you are setting background to White since you have overriden HighlightBrushKey to Transparent and selecting cell hides the cell content. So, setting Foreground to Black i guess is sufficient there. That ways you don't have to worry about not updating background of cell with background yellow already.


    But, however still you want to do that, you can do that using single MultiDataTrigger where you need to check if background of dataGridRow is Yellow, then do nothing. But since you want to do that unless background is Yellow color, use IValueConverter to see if background is other than Yellow.

    <local:MyConverter x:Key="MyConverter"/>
    //Declare local namespace with converter namespace in XAML
    
    <Style TargetType="DataGridCell">
      <Setter Property="Foreground" Value="Black" />
      <Style.Triggers>
         <MultiDataTrigger>
           <MultiDataTrigger.Conditions>
              <Condition Binding="{Binding IsSelected,
                         RelativeSource={RelativeSource Self}}" Value="True"/>
              <Condition Binding="{Binding Background,
                         RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType=DataGridRow},
                         Converter={StaticResource MyConverter}}"
                         Value="False"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="White" />
          </MultiDataTrigger>
       </Style.Triggers>
    </Style>
    

    and converter will be:

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            return System.Windows.Media.Brushes.Yellow.Equals(value);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, 
                                  System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }