25

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
    );

}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Static field of type DependencyProperty should always be public otherwise it will not work correctly. – Akash Kava Apr 13 '11 at 18:52
  • Actually, as I said, since I'm really just doing an AddOwner, you never need to access the private one since it's the shared one from the base class that you use. Still, the answer below is the correct way to do it... OverrideMetadata. That's the ticket! – Mark A. Donohoe Apr 13 '11 at 20:42

1 Answers1

43

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).

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • Was wondering though... wouldn't you have to explicitly specify all of the other properties on FrameworkPropertyMetadata as well, or only what's changed? In this case it's not much, but in cases where the AffectsMeasure or AffectsRender flags may be non-default, do I have to specify them as well, or will they just inherit their values if I don't explicitly set them? (If you know the answer, great, but I'll also work on some test code for exactly this.) – Mark A. Donohoe Oct 17 '12 at 19:15
  • 2
    @MarqueIV - Looking at the code in reflector, you do not need to set them again. Several settings are ORed together (i.e. if base sets AffectsRender or you set AffectsRender, then it affects rendering), others can be overridden (such as Inherits). – CodeNaked Oct 18 '12 at 13:46
  • 1
    I lost my copy of Reflector when I got a new MBP. Time to find it again! And so I guess it's not really 'overriding' then per se if it still has access to the original metadata (to 'or' against.) Good to know! And for what it's worth, you've personally answered or contributed to a lot of my questions. Thanks for being such a great asset to this site! Really appreciated. – Mark A. Donohoe Oct 21 '12 at 05:14
  • 1
    Just take care uf you need to set `null` then do `new FrameworkPropertyMetadata((object)null)` with type cast. Otherwise the constructor `FrameworkPropertyMetadata(PropertyChangedCallback)` is called. – xmedeko Sep 21 '17 at 20:13