如何使用数据触发器设置 WPF 行为属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30481418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set WPF behavior property using a data trigger
提问by Randeep Singh
I am trying to set a WPF behavior property using a style in the following way:
我正在尝试通过以下方式使用样式设置 WPF 行为属性:
<StackPanel>
<CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
<TextBlock>
<Hyperlink> <!--setting property directly like this: local:MyHyperLinkBehavior.Salutation="Mr." isn't working either-->
<TextBlock Text="My Hyperlink"/>
<Hyperlink.Style>
<Style TargetType="Hyperlink">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
</DataTrigger>
</Style.Triggers>
</Style>
</Hyperlink.Style>
</Hyperlink>
</TextBlock>
</StackPanel>
And the behavior class code is this:
行为类代码是这样的:
class MyHyperLinkBehavior : Behavior<Hyperlink>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += AssociatedObject_Click;
}
public static bool GetIsFemale(DependencyObject obj)
{
return (bool)obj.GetValue(IsFemaleProperty);
}
public static void SetIsFemale(DependencyObject obj, bool value)
{
obj.SetValue(IsFemaleProperty, value);
}
// Using a DependencyProperty as the backing store for IsFemale. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsFemaleProperty =
DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));
public static string GetSalutation(DependencyObject obj)
{
return (string)obj.GetValue(SalutationProperty);
}
public static void SetSalutation(DependencyObject obj, string value)
{
obj.SetValue(SalutationProperty, value);
}
// Using a DependencyProperty as the backing store for Salutation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SalutationProperty =
DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));
void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show(Convert.ToString(GetValue(SalutationProperty)));
}
}
I can't figure out why this is not working. Or setting a behavior's property using style isn't valid at all ? What is the other way round, if this is not valid.
我不明白为什么这不起作用。或者使用样式设置行为的属性根本无效?如果这无效,那相反的方法是什么。
采纳答案by Randeep Singh
I got it working, it was a small miss by me.
我让它工作了,这是我的一个小失误。
- I forgot to set the behavior on the hyperlink.
- I need to get the property of the attachedObject and not of the
behavior.
- 我忘了在超链接上设置行为。
- 我需要获取attachedObject 的属性而不是
行为的属性。
Following code works fine:
以下代码工作正常:
<StackPanel>
<CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
<TextBlock>
<Hyperlink>
<TextBlock Text="My Hyperlink"/>
<i:Interaction.Behaviors> <!--Missed setting behavior-->
<local:MyHyperLinkBehavior />
</i:Interaction.Behaviors>
<Hyperlink.Style>
<Style TargetType="Hyperlink">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
<Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
</DataTrigger>
</Style.Triggers>
</Style>
</Hyperlink.Style>
</Hyperlink>
</TextBlock>
</StackPanel>
And the behavior:
和行为:
class MyHyperLinkBehavior : Behavior<Hyperlink>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += AssociatedObject_Click;
}
public static bool GetIsFemale(DependencyObject obj)
{
return (bool)obj.GetValue(IsFemaleProperty);
}
public static void SetIsFemale(DependencyObject obj, bool value)
{
obj.SetValue(IsFemaleProperty, value);
}
// Using a DependencyProperty as the backing store for IsFemale. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsFemaleProperty =
DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));
public static string GetSalutation(DependencyObject obj)
{
return (string)obj.GetValue(SalutationProperty);
}
public static void SetSalutation(DependencyObject obj, string value)
{
obj.SetValue(SalutationProperty, value);
}
// Using a DependencyProperty as the backing store for Salutation. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SalutationProperty =
DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));
void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
// Changing "GetValue(SalutationProperty)" to "this.AssociatedObject.GetValue(SalutationProperty)" works
MessageBox.Show(Convert.ToString(this.AssociatedObject.GetValue(SalutationProperty)));
}
}
回答by Liero
There are two types of behaviorsin WPF:
WPF中有两种类型的行为:
System.Windows.InteractivityBehaviors, called also Blend Behaviours
These behaviours are classes inherited from
System.Windows.Interactivity.Behaviorand you can use them by adding to used them by Adding it to Behaviours collection, e.g:<Rectangle> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior /> </i:Interaction.Behaviors> </Rectangle>notice, that these behaviors does not have any custom attached properties. OnAttached and OnDetached methods are automatically called.
- Pros: Easy to implement
- Cons: Does not work with styles (however, it works with ControlTemplates and DataTemplates)
System.Windows.InteractivityBehaviors,也称为Blend Behaviors
这些行为是继承自的类
System.Windows.Interactivity.Behavior,您可以通过将其添加到行为集合中来使用它们,例如:<Rectangle> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior /> </i:Interaction.Behaviors> </Rectangle>请注意,这些行为没有任何自定义附加属性。OnAttached 和 OnDetached 方法会被自动调用。
- 优点:易于实施
- 缺点:不适用于样式(但是,它适用于 ControlTemplates 和 DataTemplates)
Behaviors implemented as Custom Attached Property
In these behaviors the logic defined in PropertyChangedCallback of the custom attached property.
public static readonly DependencyProperty SalutationProperty = DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(OnSalutationPropertyChanged)); private static void OnSalutationPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) { //attach to event handlers (Click, Loaded, etc...) }- Pros: Can be defined in styles, easier to use
- Cons: Chatty code, a little more difficult to implement
作为自定义附加属性实现的行为
在这些行为中,自定义附加属性的 PropertyChangedCallback 中定义的逻辑。
public static readonly DependencyProperty SalutationProperty = DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(OnSalutationPropertyChanged)); private static void OnSalutationPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) { //attach to event handlers (Click, Loaded, etc...) }- 优点:可以在样式中定义,更易于使用
- 缺点:繁琐的代码,实现起来有点困难
You are mixing those two types of behaviors together. Choose one and use it! Since you want to use it in style, you shoud choose behavior implemented as custom attached property
您将这两种行为混合在一起。选择一个并使用它!既然你想在风格中使用它,你应该选择作为自定义附加属性实现的行为

