We saw earlier how to register a new dependency property in a custom class using the DependencyProperty.Register method.  You can also reuse an existing dependency property, using the DependencyProperty.AddOwner method.

When you reuse an existing dependency property, you can optionally specify new metadata that applies to the use of the dependency property in your new type.  You should also define CLR properties in the new type that wrap the GetValue / SetValue calls to read/write dependency property values.

Here’s an example, where we reuse the BirthYearProperty , originally defined in a Person class, in a new Dog class.  Notice that we also provide a new default value.

// Dog also has a BirthYear property public static readonly DependencyProperty BirthYearProperty = Person.BirthYearProperty.AddOwner( typeof(Dog), new PropertyMetadata(2000, new PropertyChangedCallback(OnBirthYearChanged))); public int BirthYear get { return (int)GetValue(BirthYearProperty); } set { SetValue(BirthYearProperty, value); } public static void OnBirthYearChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) Jan »

Blog Stats

  • 5,264,375 hits
  • Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
    To find out more, including how to control cookies, see here: Cookie Policy