如何在WPF样式中定位不同类型的控件?

时间:2012-04-15 17:23:11

标签: wpf xaml gridsplitter

我想为水平与垂直的GridSplitter设置不同的背景。这是因为我有一个线性渐变,我需要根据网格分割器的对齐方式将其旋转90度。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type GridSplitter}">
        <Setter Property="Background" Value="Red" /> <!-- How to get this red applied to only Vertical for instance? -->
    </Style>
</ResourceDictionary>

所以问题是:我如何单独定位垂直分割器和水平分割器?

2 个答案:

答案 0 :(得分:3)

使用Style.Triggers有条件地应用设置器。

答案 1 :(得分:1)

好的,好像我明白了:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type GridSplitter}">
        <Style.Triggers>
            <Trigger Property="VerticalAlignment" Value="Stretch">
                <Setter Property="Background" Value="#F7F7F7" />
            </Trigger>

            <Trigger Property="HorizontalAlignment" Value="Stretch">
                <Setter Property="Background" Value="red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>