我在使用DataTrigger在“选择”时可视地更改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);
}
}
}
这很奇怪,因为绑定到Label的Content属性是有效的,但是对DataTrigger的绑定没有触发。我已经尝试将Mode = OneWay添加到DataTrigger绑定中,但它没有什么区别。
修改
我添加了一个不同的Setter属性“Margin”,并且该属性有效 因此,出于某种原因,Setter似乎无法覆盖Border的现有属性。谁知道为什么?
答案 0 :(得分: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>