Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I assume you meant DependencyProperty.Register and DependencyProperty.RegisterAttached.

DependencyProperty.Register is used to register normal DependencyProperty. You can see those as just regular properties, with the added twist that they can take part in WPF's DataBinding, animations etc. In fact, they are exposed as normal property (with the get and set accessors) on top of the untyped DependencyObject.SetValue / GetValue. You declare those as part of your type.

Attached properties on the other hand are different. They are meant as an extensibility system. If you have ever used Extenders in Windows Forms, they are kind of similar. You declare them as part of a type, to be used on another type.

They are used a lot for layout-related information. For example, Canvas needs Left/Top coordinates, Grid needs a Row and a Column, DockPanel needs a Dock information etc. It would be a mess if all of this had to be declared on every Control that can be layouted. So they are declared on the corresponding panel, but used on any Control.

You can use the same thing to attach any information to a DependencyObject if you need to. It can come in handy to just declare a piece of information that you can set in xaml just to be used later in a style for an existing class for example.

So those two kind of DependencyProperty serve a very different purpose. Regular properties (registered through Register() ) are used just like normal properties as part of the interface of your type. Attached properties (registered through RegisterAttached() ) are used as an extensibility point on existing classes.

Hope that clarifies it a bit.

So attach can be used to add properties to classes that you don't own? Is that the difference? Vaccano May 12, 2010 at 16:16 You can set dependency properties that are not attached properties on other object too: this.button.SetValue(TextBox.TextProperty, "text"); (where this.button is of type System.Windows.Controls.Button ). This is perfectly fine so the answer is not quite complete. There must be more to it. bitbonk Aug 7, 2014 at 9:39

The difference between DependencyProperty.Register() and DependencyProperty.RegisterAttached() is that .Register() is used to register a 'regular' dependency property on a DependencyObject, while .RegisterAttached() is used to set an 'attached' dependency property.

The difference between the two types of dependency properties is reasonably straightforward: regular dependency properties are set on a particular DependencyObject just like you would any other .NET property. Attached properties, on the other hand, are associated with a particular DependencyObject (e.g. Grid) but are set on a completely separate DependencyObject, often a child of the DependencyObject that defines the attached property (e.g. Grid.Row, an attached property, is set on the children of a parent Grid).

More details on attached properties are on MSDN .

A property registered with either Register or RegisterAttached can be attached to any DependencyObject with SetValue and GetValue. But if you attach a property registered with Register to an object of type other than the ownerType, its metadata will not be used (except for a default value). It means that attributes such as Inherits or AffectsMeasure will not work for these properties. You should use RegisterAttached if you are interested in metadata on attached properties.

For details, see my answer to a similar question: Difference between Attached and non-Attached Dependency Properties in Silverlight

In my case RegisterAttached only set the bound field once, while Register worked dynamically as intended.

Taken from a scenario, where a Button in a List needed to show a preview on MouseHover and CommandParam was taken, already.

public static DependencyProperty MouseCommandParamProperty = DependencyProperty.Register(
                                                                        "MouseCommandParam",
                                                                        typeof(object),
                                                                        typeof(MouseBehaviour),
                                                                        new PropertyMetadata(defaultValue:null, new PropertyChangedCallback(MouseCommandParamChanged)));
        public static void SetMouseCommandParam(DependencyObject target, object value)
            target.SetValue(MouseBehaviour.MouseCommandParamProperty, value);
        public static object GetMouseCommandParam(DependencyObject target)
            return (object)target.GetValue(MouseBehaviour.MouseCommandParamProperty);
        private static void MouseCommandParamChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
            UIElement element = target as UIElement;
            if (element != null)
                element.SetValue(MouseBehaviour.MouseCommandParamProperty, e.NewValue);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.