I would like to implement a set of similar applied actions for use in a WPF application. Since they all share a piece of template code that I really don't want to repeat for everyone, I would like to create a basic behavior inherited from it. But since everything inside the attached behavior is static, I donβt understand how to do it.
As an example, take this behavior that the method executes in mousedown (the real behavior, of course, will do something that is not easy to do in the event handler):
public static class StupidBehavior { public static bool GetIsEnabled(DependencyObject obj) { return (bool)obj.GetValue(IsEnabledProperty); } public static void SetIsEnabled(DependencyObject obj, bool value) { obj.SetValue(IsEnabledProperty, value); } // Using a DependencyProperty as the backing store for ChangeTooltip. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(StupidBehavior), new UIPropertyMetadata(false, IsEnabledChanged)); private static void IsEnabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { ((UIElement)sender).MouseDown += { (o,e) => MyMethod(); }; } private static void MyMethod() { MessageBox.Show("Boo"); } }
Now I would like to create a new behavior, which should have a different implementation of MyMethod, as well as some additional properties that control it. How to do it?
source share