[英]DataTrigger binding to UserControl dependency property
使用DataTrigger在“选定”用户控件时以可视方式更改它时遇到麻烦。
我在名为“ 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”,并且该属性有效。
因此,由于某些原因,二传手们似乎无法覆盖边框的现有属性。 有人知道为什么吗?
说明在这里: http : //msdn.microsoft.com/zh-cn/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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.