Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default value of an inherited dependency property?

Tags:

How can I change the default value for an inherited dependency property? In our case, we've created a subclass of Control which by default has its Focusable set to 'true'. We want our subclass to have the default of 'false'.

What we've been doing is simply setting it to 'false' in the constructor, but if someone uses ClearValue, it goes back to the default, not the value set in the constructor.

Here's what I'm currently doing to achieve this (This is a test control with a DP of 'Foo' for an example.) I'm not a fan of the 'new' to hide the property although thanks to AddOwner, it does point to the same shared instance so I guess it's ok. It looks like it inherits all the other metadata values as well so that's good. Just wondering if this is correct?

public class TestControlBase : Control {      public static readonly DependencyProperty FooProperty = DependencyProperty.Register(         "Foo",         typeof(int),         typeof(TestControlBase),         new FrameworkPropertyMetadata(4) // Original default value     );      public int Foo     {         get { return (int)GetValue(FooProperty); }         set { SetValue(FooProperty, value); }     }  }  public class TestControl : TestControlBase {      public static readonly new DependencyProperty FooProperty = TestControlBase.FooProperty.AddOwner(         typeof(TestControl),         new FrameworkPropertyMetadata(67) // New default for this subclass     );  } 

Mark

UPDATE...

I think this is even better as it eliminates the 'new' call. You still access it via the FooProperty on the base class since this uses AddOwner. As such, it's technically the same one.

public class TestControl : TestControlBase {     // Note this is private     private static readonly DependencyProperty AltFooProperty = TestControlBase.FooProperty.AddOwner(         typeof(TestControl),         new FrameworkPropertyMetadata(67) // New default for this subclass     );  } 
like image 639
Mark A. Donohoe Avatar asked Apr 13 '11 17:04

Mark A. Donohoe


People also ask

How do I set default value for dependency property?

When you define a custom XAML dependency property, one of the things you do is specify the default value. You can do this by providing the default value directly: DependencyProperty. Register("MyProperty", propertyType, ownerType, new PropertyMetadata(defaultValue));

How do I override metadata?

When calling OverrideMetadata(Type, PropertyMetadata), a derived class specifies its own type as the first parameter, and a metadata instance as the second parameter. A derived class that overrides metadata on a dependency property must do so before the property is placed in use by the property system.

What is Property metadata?

Remarks. Property metadata can be defined and used during dependency property registration when calling the Register method (or variations for attached properties or read-only dependency properties), or after original owner registration when calling the OverrideMetadata method.

What is a dependency property?

A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.


1 Answers

The correct way to override a base class's property is:

static TestControl() {      FooProperty.OverrideMetadata(         typeof(TestControl),         new FrameworkPropertyMetadata(67)     ); } 

EDIT:

AddOwner is meant to share the same DependencyProperty across types that are not related (i.e. the TextProperty of TextBox and TextBlock).

like image 64
CodeNaked Avatar answered Sep 20 '22 19:09

CodeNaked