我有一个 UserControl
,它有两个自定义属性 CustomA
和 CustomB
. 我想在 UserControl
中的 Label
控件中使用DataTriggers来更改这些自定义属性的 Value
.
在我的示例中,我可以_1486824知道如何在Setter中访问 CustomB
属性,这样当DataTrigger中的 CustomA
属性发生变化时,我可以更改 Value
. 我认为 CustomA
属性的绑定是正确的,但我不知道如何使用Setter来访问 CustomB
.
总结一下,我需要知道如何从控件的Style DataTriggers中访问属于我的 UserControl
的自定义属性 - 在本例中是 Label
- 并且还可以更改它们的值
UCLabel.xaml - UserControl
<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 - Code Behind
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
1 回答
在Style中使用
Setter
时,表示更改当前控件的属性(在您的情况下,它是Label),并且不能更改任何其他控件的属性 . 但是,您可以通过Behaviors
实现目标 .首先,您需要将以下程序集引用添加到项目中:
然后使用以下代码: