简体   繁体   English

WPF XAML如何为datagrid编写触发器或多重触发器?

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

1 if you select the cell, set row background on a white colour 1如果选择单元格,请将行背景设置为白色

and

2 if you select the cell and row background is yellow, does not change the row background 2如果选择单元格并且行背景为黄色,则不会更改行背景

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 ? 也许我需要一个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>

I guess you are setting background to White since you have overriden HighlightBrushKey to Transparent and selecting cell hides the cell content. 我猜你正在设置White背景,因为你已经将HighlightBrushKey覆盖为Transparent并选择单元格隐藏了单元格内容。 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. 但是,无论你想要做什么,你都可以使用单个MultiDataTrigger来做,你需要检查dataGridRow的背景是否为黄色,然后什么也不做。 But since you want to do that unless background is Yellow color, use IValueConverter to see if background is other than Yellow . 但是,除非背景为黄色,否则您需要这样做, 请使用IValueConverter查看背景是否不是黄色

<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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM