How to set WPF behavior property using data trigger

I am trying to set a WPF behavior property using a style like this:

<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 code for the behavior class 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 understand why this is not working. Or is setting a behavior property using a style invalid at all? What will be the other way around if this is not true.

+6
source share
2 answers

I got a job , it was a small mission.

  • I forgot to set the behavior in the hyperlink.
  • I need to get the property of the attached object, not the behavior.

The 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))); } } 
+3
source

There are two types of behavior in WPF:

  • System.Windows.Interactivity Behavior, also called Blend Behaviors

    These behaviors are classes inherited from System.Windows.Interactivity.Behavior , and you can use them by adding them to their use by adding it to the Behaviors collection, for example:

     <Rectangle> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior /> </i:Interaction.Behaviors> </Rectangle> 

    note that these behaviors do not have any custom properties. The OnAttached and OnDetached methods are automatically called.

    • Advantages : easy to implement
    • Cons : doesn't work with styles (however it works with ControlTemplates and DataTemplates)
  1. Behavior Implemented as a Custom Attached Property

    In this behavior, the logic defined in the CustomChangedCallback property of the custom 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 harder to implement

You mix these two types of behavior together. Choose one and use it! Since you want to use it in style, you choose a behavior implemented as a custom attached property

+7
source

Source: https://habr.com/ru/post/988007/


All Articles