如何更改继承依赖项属性的默认值?

时间:2023-01-24 17:20:44

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

如何更改继承的依赖项属性的默认值?在我们的例子中,我们已经创建了控件的子类,默认情况下,它的可聚焦设置为“true”。我们希望我们的子类具有默认的'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.

我们所做的只是在构造函数中将它设置为“false”,但是如果有人使用ClearValue,它会返回到默认值,而不是构造函数中的值集。

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?

下面是我目前为实现这个目标所做的事情(例如,这是一个带有DP of 'Foo'的测试控件)。我不喜欢用“new”来隐藏这个属性,不过多亏了AddOwner,它确实指向了相同的共享实例,所以我想可以。看起来它还继承了所有其他的元数据值,这很好。想知道这对不对?

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.

我认为这甚至更好,因为它消除了“新的”调用。您仍然可以通过基类上的FooProperty访问它,因为它使用AddOwner。因此,它在技术上是相同的。

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

}

1 个解决方案

#1


39  

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

AddOwner是指跨不相关的类型共享相同的DependencyProperty(即TextBox和TextBlock的TextProperty)。

#1


39  

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

AddOwner是指跨不相关的类型共享相同的DependencyProperty(即TextBox和TextBlock的TextProperty)。