Wpf 触发器的UserControl自定义属性
我正在编写一个自定义用户控件,称为MyUserControl。我在主窗口中使用了许多dependecProperties,其中多次定义了几个MyUserControl。我想知道的是,如何创建样式的触发器/属性将触发的自定义属性 例如,如果我有一个自定义属性BOOL isgo和一个自定义属性MyBackgroung(UserControl的背景),两者都定义为:Wpf 触发器的UserControl自定义属性,wpf,triggers,dependencies,styles,custom-properties,Wpf,Triggers,Dependencies,Styles,Custom Properties,我正在编写一个自定义用户控件,称为MyUserControl。我在主窗口中使用了许多dependecProperties,其中多次定义了几个MyUserControl。我想知道的是,如何创建样式的触发器/属性将触发的自定义属性 例如,如果我有一个自定义属性BOOL isgo和一个自定义属性MyBackgroung(UserControl的背景),两者都定义为: public bool IsGoing { get { return (bool)this.GetValue(I
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,这取决于IsGong属性是否为true/false?
我尝试了很多事情,但本质上,我正在努力实现以下目标:
<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>
我希望我的解释能让你理解。我已经为此工作了几天了,但似乎找不到解决办法。
谢谢你的帮助
Adrian您的样式应该只需要用作
UserControl.style
,并且具有正确的TargetType
,您打算通过触发器更改的默认值也需要移动到样式中,原因是:
这是否真正起作用取决于您如何使用控件定义中的属性。谢谢!!!!它成功了:)。。。哇,我真不敢相信这么简单。在挣扎了几天之后,事实上是一样的,只是修改了几行。。。我不敢相信。。。谢谢
<custom:MyUserControl.Style>
<Style TargetType="custom:MyUserControl">
<Setter Property="MyBackground" Value="Green"/>
<Style.Triggers>
<Trigger Property="IsGoing" Value="True">
<Setter Property="MyBackground" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</custom:MyUserControl.Style>