首页 文章

DataTrigger绑定到UserControl依赖项属性

提问于
浏览
2

我无法使用DataTrigger在"selected"时可视地更改UserControl .
我在名为"IsSelected"的类中创建了一个'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);
        }
    }

}

它's strange because the binding to the Label'的Content属性正在运行,但是没有触发与DataTrigger的绑定 . 我已经尝试将Mode = OneWay添加到DataTrigger绑定,但它没有什么区别 .

Edit:

我添加了一个不同的Setter属性"Margin",该属性工作 .
因此,出于某种原因,Setter似乎无法覆盖Border的现有属性 . 谁知道为什么?

1 回答

  • 6

    说明如下: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>
    

相关问题