我的 WPF 应用程序中有一个DataGrid
控件。我有一个DataGridRow
运行良好的类的默认自定义样式。但是,对于这一DataGrid
特定屏幕上的这一特定,我需要不同的自定义样式。
每行中的项目都有一个bool
属性,当设置时,我想用不同的前景色和背景色显示该行。However, when the row is selected AND when that property is set, I want a different foreground and background color to show that it's selected AND the property is set to true.
这是我尝试过的:
<Style TargetType="DataGridRow" x:Key="CameraStyle">
<Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="IsSelected" Value="False" />
<Condition Binding="{Binding Path=IsInLiveMode}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="Red" />
<Setter Property="Background" Value="Yellow" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Binding="{Binding Path=IsInLiveMode}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="DarkOrange" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="Foreground" Value="DarkRed" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
这给了我一个“绑定必须为非空”错误,我认为这是因为Binding
MultiDataTrigger 中的第一个条件没有属性。
在 XAML 中编写此代码的正确方法是什么?
编辑:
在尝试了 nemesv 和 Rachel 的答案后,代码现在可以编译并运行。但是,我为 IsSelected = true 和 IsInLiveMode = true 案例选择的颜色没有显示出来。这是我现在拥有的:
<Style TargetType="DataGridRow" x:Key="CameraStyle">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="False" />
<Condition Binding="{Binding Path=IsInLiveMode}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Red" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding Path=IsInLiveMode}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource DataBackgroundSelected}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="Foreground" Value="{DynamicResource DataForegroundSelected}" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding Path=IsInLiveMode}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="DarkOrange" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="Foreground" Value="DarkRed" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
关于为什么有问题的案例不起作用的任何想法?