Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is wrong to believe that "RegisterAttached allows the value to be assigned to any dependency object whereas Register only allows it to be attached to the class passed as the ownerType parameter". Here is a perfectly working example of an attached property registered with Register:</p> <pre><code>class FooPropertyDeclaringType { public static readonly DependencyProperty FooProperty = DependencyProperty.Register("Foo", typeof(int), typeof(FooPropertyDeclaringType)); } class SomeUnrelatedType : DependencyObject { } class Program { static void Main() { var obj = new SomeUnrelatedType(); obj.SetValue(FooPropertyDeclaringType.FooProperty, 10); Debug.Assert(10 == (int)obj.GetValue(FooPropertyDeclaringType.FooProperty)); } } </code></pre> <p>Reflector shows that the only difference between Register and RegisterAttached is that the Register throws out much of the supplied metadata and only preserves it for the instances of registering class (via OverrideMetadata). It means that attributes such as Inherits and various update notifications usually specified in the metadata do not work on properties registered with Register and attached to objects of other types (other than the registering type). So Register is actually a stripped down version of RegisterAttached. It was probably made this way for performance reasons.</p> <p>In the <a href="http://blogs.msdn.com/b/bencon/archive/2006/07/24/677520.aspx" rel="noreferrer">example</a> linked by Haris Hasan in the comments to his answer, if you change RegisterAttached to Register, the buttons stop moving (because the property no longer provides AffectsParentArrange metadata for Button type) but they are nevertheless redrawn in their new locations when you resize the window. But if you add the same metadata to the Button type after a call to InitializeComponent():</p> <pre><code>RadialPanel.AngleProperty.OverrideMetadata( typeof(Button), new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsParentArrange)); </code></pre> <p>then everything works again as if RegisterAttached was called.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload