15

我的 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>

这给了我一个“绑定必须为非空”错误,我认为这是因为BindingMultiDataTrigger 中的第一个条件没有属性。

在 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>

关于为什么有问题的案例不起作用的任何想法?

4

2 回答 2

19

您关于缺少绑定的假设是正确的。

从 MSDN MultiDataTrigger.Conditions

对于 MultiDataTrigger,集合中的每个条件都必须设置 Binding 和 Value属性。

您可以使用RelativeSource Self在绑定中引用自己来解决此问题:

<MultiDataTrigger.Conditions>
    <Condition Binding="{Binding RelativeSource={RelativeSource Self},
         Path=IsSelected}" Value="True" />
    <Condition Binding="{Binding Path=IsInLiveMode}" Value="True" />
</MultiDataTrigger.Conditions>
于 2012-08-28T13:16:13.513 回答
8

您正在使用 a MultiDataTrigger,它仍然是 aDataTrigger并且需要绑定

切换到Property="IsSelected"_Condition

<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" 
           Value="True" />
于 2012-08-28T13:16:23.083 回答