#154 – Reusing an Existing Dependency Property in Your Class
December 13, 2010 1 Comment
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) { }
Whats the use of this, it still requires same typing.