Datatriggers 在子控件的样式中访问和设置 UserControl 的自定义属性
Datatriggers access and set custom Properties of a UserControl within the style of a child Control
我有一个 UserControl
,它有两个自定义属性 CustomA
和 CustomB
。我想在 UserControl
中的 Label
控件中使用 DataTriggers 来更改这些自定义属性的 Value
。
在我的示例中,我似乎无法或不知道如何访问 Setter 中的 CustomB
属性,以便我可以更改它的 Value
当 CustomA
属性 的 Value
在 DataTrigger 中发生变化时。我认为 CustomA
属性 的绑定是正确的,但我不知道 Setter 使用什么来访问 CustomB
.
总而言之,我需要知道如何从控件的 Style DataTriggers 中访问属于我的 UserControl
的自定义属性 - 在本例中为 Label
-并更改它们的值
UCLabel.xaml - 用户控件
<UserControl x:Class="UCLabel"
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"
xmlns:local="clr-namespace:TestProgram"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="100">
<Label Name="lbl">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=CustomA}" Value="True">
<Setter Property="CustomB" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</UserControl>
UCLabel.xaml.vb - 代码隐藏
Imports System.Windows
Public Class UCLabel
'CustomA'
Public Shared ReadOnly CustomAProperty As DependencyProperty =
DependencyProperty.Register("CustomA",
GetType(Boolean),
GetType(UCLabel), New PropertyMetadata(False))
Public Property CustomA As Boolean
Get
Return CBool(GetValue(CustomAProperty))
End Get
Set(ByVal value As Boolean)
SetValue(CustomAProperty, value)
End Set
End Property
'CustomB'
Public Shared ReadOnly CustomBProperty As DependencyProperty =
DependencyProperty.Register("CustomB",
GetType(Boolean),
GetType(UCLabel), New PropertyMetadata(False))
Public Property CustomB As Boolean
Get
Return CBool(GetValue(CustomBProperty))
End Get
Set(ByVal value As Boolean)
SetValue(CustomBProperty, value)
End Set
End Property
End Class
在Style中使用Setter
时,表示改变当前控件(你的是Label)的属性,不能改变任何控件的属性其他控件。但是,您可以通过 Behaviors
.
实现您的目标
首先,您需要将以下程序集引用添加到您的项目中:
System.Windows.Interactivity
Microsoft.Expression.Interactions
然后使用下面的代码:
<UserControl
x:Name="uc"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Label>
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding ProA, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">
<ei:ChangePropertyAction
PropertyName="ProB"
TargetName="uc"
Value="True" />
</ei:DataTrigger>
</i:Interaction.Triggers>
</Label>
我有一个 UserControl
,它有两个自定义属性 CustomA
和 CustomB
。我想在 UserControl
中的 Label
控件中使用 DataTriggers 来更改这些自定义属性的 Value
。
在我的示例中,我似乎无法或不知道如何访问 Setter 中的 CustomB
属性,以便我可以更改它的 Value
当 CustomA
属性 的 Value
在 DataTrigger 中发生变化时。我认为 CustomA
属性 的绑定是正确的,但我不知道 Setter 使用什么来访问 CustomB
.
总而言之,我需要知道如何从控件的 Style DataTriggers 中访问属于我的 UserControl
的自定义属性 - 在本例中为 Label
-并更改它们的值
UCLabel.xaml - 用户控件
<UserControl x:Class="UCLabel"
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"
xmlns:local="clr-namespace:TestProgram"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="100">
<Label Name="lbl">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=CustomA}" Value="True">
<Setter Property="CustomB" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</UserControl>
UCLabel.xaml.vb - 代码隐藏
Imports System.Windows
Public Class UCLabel
'CustomA'
Public Shared ReadOnly CustomAProperty As DependencyProperty =
DependencyProperty.Register("CustomA",
GetType(Boolean),
GetType(UCLabel), New PropertyMetadata(False))
Public Property CustomA As Boolean
Get
Return CBool(GetValue(CustomAProperty))
End Get
Set(ByVal value As Boolean)
SetValue(CustomAProperty, value)
End Set
End Property
'CustomB'
Public Shared ReadOnly CustomBProperty As DependencyProperty =
DependencyProperty.Register("CustomB",
GetType(Boolean),
GetType(UCLabel), New PropertyMetadata(False))
Public Property CustomB As Boolean
Get
Return CBool(GetValue(CustomBProperty))
End Get
Set(ByVal value As Boolean)
SetValue(CustomBProperty, value)
End Set
End Property
End Class
在Style中使用Setter
时,表示改变当前控件(你的是Label)的属性,不能改变任何控件的属性其他控件。但是,您可以通过 Behaviors
.
首先,您需要将以下程序集引用添加到您的项目中:
System.Windows.Interactivity
Microsoft.Expression.Interactions
然后使用下面的代码:
<UserControl
x:Name="uc"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Label>
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding ProA, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Value="True">
<ei:ChangePropertyAction
PropertyName="ProB"
TargetName="uc"
Value="True" />
</ei:DataTrigger>
</i:Interaction.Triggers>
</Label>