Share via


WPF: Tips - Simple Attached Dependency Property

This article explains how to define and use a basic Attached Property.


Introduction

This article is intended as a quick reminder for the more experienced developer or an introduction for the beginner.

Define A Class

First thing to do is to define a class. If this is to be attached to a DependencyObject like a control then the class doesn't need to inherit from DependencyObject itself.

public class  Attach
{
    public static  string GetMyString(FrameworkElement ctrl)
    {
        return (string)ctrl.GetValue(MyStringProperty);
    }
    public static  void SetMyString(FrameworkElement ctrl,  string  value)
    {
        ctrl.SetValue(MyStringProperty, value);
    }
    public static  DependencyProperty MyStringProperty = DependencyProperty.RegisterAttached(
                      "MyString",
                      typeof(string),
                      typeof(Attach),
    new FrameworkPropertyMetadata("Default", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
        );
}

That class can be called pretty much anything you like instead of Attach.
As you can see this is just a plain class. Everything else is static and that can present some challenges in more advanced situations but we're just adding a string property so that's no problem here.

The property is called MyString and that's prefixed with get and set for the methods.  The DependencyProperty suffixes with Property instead and both those methods refer to that.
Note that the parameter setting the type of the parent refers to Atatch rather than whatever you're going to actually attach the property to.
One fiddly bit here is that FrameworkPropertyMetadata which give our string property a default value of "Default" and make it's default binding mode two way.

Watch out for the default value you're given if you use the propdp snippet, as explained here.

MarkUp

Using that is fairly simple, but there's a trick to referencing the property in a binding.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350"  Width="525">
    <Grid Name="root" local:Attach.MyString="Something">
        <TextBlock Text="{Binding Path=(local:Attach.MyString), ElementName=root}"/>
    </Grid>
</Window>

You need an xmlns reference to the project namespace in order to reference Attach. The project will also have to be compiled before the designer will find it. You'll get blue squiggly lines untill then. It's usually best to define your class and property first, compile and then add to any markup.
The tricky bit there is in the Binding. You need round brackets round the class and variable like you see there. Without those round brackets, you will find it errors.

Conclusion

Not much to an attached property but it's pretty strange code if you've never used one. Hopefully this article will help developers avoid the more obvious problems when they use an Attached Dependency Property.

See Also

More WPF Tips