Behaviors are a powerful way to encapsulate functionality and make it available for designers to use in Blend 3. As your library of custom behaviors grows, you might notice a number of properties that most of your behaviors use. You know that duplication of each of these properties in each of your behaviors is not the right way to go, so you want to create a base class that all your custom behaviors use.
The TriggerAction base class includes an IsEnabled property which I would like to include in all my custom behaviors. Instead of adding the DependencyProperty and associated code to each custom behavior, let’s create a base behavior class.
The existing behavior base classes are:

If we create a custom behavior, MyBehavior, that inherits from Behavior<DependencyObject> and in Blend 3 drop the behavior on the LayoutRoot Grid, we get a behavior with no properties defined:


To create a base class for our custom behaviors, create an abstract BaseBehavior class that inherits from Behavior<T>. This is where we add the IsEnabled property and any other common properties we want our custom behaviors to have. Next derive a BaseBehavior<T> class from BaseBehavior. It is from BaseBehavior<T> that we will derive our custom behaviors:
The code for BaseBehavior simply derives from Behavior<T> and defines the property, dependency property, and callback method for the IsEnabled property:
public abstract class BaseBehavior : Behavior<DependencyObject>
{
internal BaseBehavior()
{
}
#region IsEnabled (Dependency Property)
[Category("Common Properties")]
public bool IsEnabled
{
get
{
return (bool)base.GetValue(IsEnabledProperty);
}
set
{
base.SetValue(IsEnabledProperty, value);
}
}
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.Register(
"IsEnabled",
typeof(bool),
typeof(BaseBehavior),
new PropertyMetadata(true, new PropertyChangedCallback(OnIsEnabledChanged)));
private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((BaseBehavior)d).OnIsEnabledChanged(e);
}
protected virtual void OnIsEnabledChanged(DependencyPropertyChangedEventArgs e)
{
}
#endregion
}
The BaseBehavior<T> class derives from BaseBehavior and redefines the AssociatedObject property using the new keyword. We do this so that the AssociatedObject can be typed when we derive our custom behavior from it:
public abstract class BaseBehavior<T>
: BaseBehavior where T : DependencyObject
{
protected BaseBehavior() : base() {}
protected new T AssociatedObject
{
get
{
return (T)base.AssociatedObject;
}
}
}
In MyBehavior, change the class definition from:
public class MyBehavior : Behavior<DependencyObject>
to
public class MyBehavior : BaseBehavior<DependencyObject>
BaseBehavior<T> is a drop-in replacement for Behavior<T>. When we recompile and view the behavior’s properties in Blend, we see that the IsEnabled property has been added:

As needed, we can add additional properties to BaseBehavior.
What other properties do you think should be added to BaseBehavior?

