我正在编写一个名为 MyUserControl 的自定义用户控件。我有很多 DependecyProperties,我在 MainWindow 中使用了多个 MyUserControl 被多次定义的地方。我想知道的是如何创建样式的触发器/属性会触发的自定义属性?
例如,如果我有一个自定义属性 BOOL IsGoing 和一个自定义属性 MyBackgroung(UserControl 的背景),两者都定义为:
public bool IsGoing
{
get { return (bool)this.GetValue(IsGoingProperty); }
set { this.SetValue(IsGoingProperty, value); }
}
public static readonly DependencyProperty IsGoingProperty = DependencyProperty.RegisterAttached(
"IsGoing", typeof(bool), typeof(MyUserControl), new PropertyMetadata(false));
public Brush MyBackground
{
get { return (Brush)this.GetValue(MyBackgroundProperty); }
set { this.SetValue(MyBackgroundProperty, value); }
}
public static readonly DependencyProperty MyBackgroundProperty = DependencyProperty.Register(
"MyBackground", typeof(Brush), typeof(MyUserControl), new PropertyMetadata(Brushes.Red));
如果我在 MainWindow.xaml 中定义我的 UserControl,我如何访问触发器并设置 MyBackground,具体取决于 IsGoing 属性是否为真/假?我尝试了很多事情,但本质上,我正在尝试实现以下目标:
<custom:MyUserControl MyBackground="Green" x:Name="myUC1" Margin="120.433,0,0,65.5" Height="50" Width="250" VerticalAlignment="Bottom" HorizontalAlignment="Left" >
<Style>
<Style.Triggers>
<Trigger Property="IsGoing" Value="True">
<Setter Property="MyBackground" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</custom:MyUserControl>
我希望我的解释足以让你理解。我已经为此工作了几天,但似乎找不到解决方案。谢谢您的帮助!!!
阿德里安