One of the powerful concept of WPF is Attached Properties. The primary purpose of attached property is to define a unique value to child elements for a property, which is defined in Parent element as MSDN suggests. Some of the typical examples are Grid.Row, DockPanel.Dock, etc.

But it can also be used to attach additional information to an object, like Extension methods in C#. Examples are allowing an object to be draggable using Mouse, play music on some action, etc. I will take a simple piece of functionality to explain this. Lets consider, we need to focus a control on loaded.

Using Attached Property

    public static class FocusOnLoad
{
public static bool GetCanFocusOnLoad(DependencyObject obj)
{
return (bool)obj.GetValue(CanFocusOnLoadProperty);
}

public static void SetCanFocusOnLoad(DependencyObject obj, bool value)
{
obj.SetValue(CanFocusOnLoadProperty, value);
}

// Using a DependencyProperty as the backing store for CanFocusOnLoad. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CanFocusOnLoadProperty =
DependencyProperty.RegisterAttached("CanFocusOnLoad", typeof(bool), typeof(FocusOnLoad), new PropertyMetadata(FocusOnLoadChanged));

private static void FocusOnLoadChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as FrameworkElement;
if (element != null)
{
element.Loaded += delegate
{
element.Focus();
};
}
}
}

Usage in XAML

    <TextBox behaviors:FocusOnLoad.CanFocusOnLoad="True"
VerticalAlignment="Center"
Width="200"/>

To make the work more systematic, Microsoft came up with an option called Behaviors. It offers more encapsulated way to extend functionality. There are some built-in behaviors shipped as part of Blend. Following code implements the same functionality using Behaviors.

Using Behaviors

    public class FocusOnLoadBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += this.OnLoaded;
}

protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= this.OnLoaded;
}

private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
{
this.AssociatedObject.Focus();
}
}

Usage in XAML

    <TextBox behaviors:FocusOnLoad.CanFocusOnLoad="True"
VerticalAlignment="Center"
Width="200">
<i:Interaction.Behaviors>
<behaviors:FocusOnLoadBehavior />
</i:Interaction.Behaviors>
</TextBox>

There are some advantages and dis-advantages in both approach. Let me put it down to make the decision easier.

1. Event Handlers

Both scenario need an event subscription to the Control.Loaded event. In attached properties it is an ugly code, where we need to write extra plumbing to release those handlers. But behaviors offer a clean way to release event handlers – OnAttached and OnDetaching. If the functionality dealing with more event handling mechanisms, Behaviors are the best way.

2. Configuration in Style

For example, if you want to attach this piece of functionality to all the TextBoxes in your application, a global style would be the best option. But attached properties can be set in Style, where Behaviors cannot. This is also an important factor need to be considered before taking decision.

<Style TargetType="TextBox">
<Setter Property="behaviors:FocusOnLoad.CanFocusOnLoad"
Value="True" />
</Style>

3. Casting

The PropertyChanged callback will give you the dependency object. This is the element in XAML, where we set the property. But to access some property or to invoke a method we need to cast it to UIElement or FrameworkElement. But in Behavior, it is possible to mention the datatype – Behavior<T>. So no more casting. Using AssociateObject all the members of the type can be accessed. In above case, I mentioned it as FrameworkElement, since the Loaded event coming from that.

4. Visual Designer

Behaviors can be drag and drop into particular object using Blend, where Attached properties are not.You can drag and drop behavior objects on other behavior objects to set up a hierarchy, and you can set properties on objects already in the designer through the properties window.

Hope this will shed some light on choosing a right option. 🙂