DataTrigger 绑定到 UserControl 依赖属性

DataTrigger binding to UserControl dependency property

我在使用 DataTrigger 以可视方式更改 UserControl 时遇到问题 "selected"。
我在名为 "IsSelected" 的 class 中创建了一个 'bool' 依赖属性。

我正在使用这个 XAML:

<UserControl x:Name="zControl"
             x:Class="Intel.AdaptivePerformance.Client.UI.Controls.ZoomControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignWidth="50" d:DesignHeight="32">
    <Border BorderBrush="Black" BorderThickness="1,0,1,2" Background="#2F000000">
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                        <Setter Property="BorderThickness" Value="1,0,1,0" />
                        <Setter Property="Background" Value="{x:Null}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
    </Border>
</UserControl>

并使用此 C#:

public partial class ZoomControl : UserControl {

    public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(ZoomControl), new PropertyMetadata(false));
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ZoomControl), new PropertyMetadata(null));

    public ZoomControl() {
        InitializeComponent();
    }

    public bool IsSelected {
        get {
            return (bool) GetValue(IsSelectedProperty);
        }
        set {
            SetValue(IsSelectedProperty, value);
        }
    }

    public string Text {
        get {
            return (string) GetValue(TextProperty);
        }
        set {
            SetValue(TextProperty, value);
        }
    }

}

这很奇怪,因为与 Label 的 Content 属性的绑定有效,但与 DataTrigger 的绑定没有触发。我已尝试将 Mode=OneWay 添加到 DataTrigger 绑定,但它没有任何区别。

编辑:

我添加了一个不同的 Setter 属性 "Margin" 并且该属性有效。
因此,出于某种原因,Setter 似乎无法覆盖 Border 的现有属性。有人知道为什么吗?

解释在这里: http://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx#multiple_sets 本地值优先于触发器。
所以尝试改变这个:

<Border BorderBrush="Black" BorderThickness="1,0,1,2" Background="#2F000000">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                    <Setter Property="BorderThickness" Value="1,0,1,0" />
                    <Setter Property="Background" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
</Border>

至:

<Border BorderBrush="Black">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Setter Property="Background" Value="#2F000000" />
            <Setter Property="BorderThickness" Value="1,0,1,2" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=zControl, Path=IsSelected}" Value="True">
                    <Setter Property="BorderThickness" Value="1,0,1,0" />
                    <Setter Property="Background" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <Label Content="{Binding ElementName=zControl, Mode=OneWay, Path=Text}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" />
</Border>