如何更改继承的依赖属性的默认值?
如何更改继承的依赖属性的默认值?在我们的例子中,我们创建了 Control 的一个子类,默认情况下它的 Focusable
设置为“true”。我们希望我们的子类具有默认值“false”。
我们所做的只是在构造函数中将其设置为“false”,但如果有人使用 ClearValue,它会返回到默认值,而不是构造函数中设置的值。
以下是我目前为实现此目标所做的事情(例如,这是一个 DP 为“Foo”的测试控件。)尽管感谢 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
);
}
马克
更新...
我认为这更好,因为它消除了“新”调用。您仍然可以通过基类上的 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
);
}
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
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
覆盖基类属性的正确方法是:
编辑:
AddOwner
旨在跨不相关的类型(即TextProperty
)共享相同的DependencyProperty
TextBox
和TextBlock
的代码>)。The correct way to override a base class's property is:
EDIT:
AddOwner
is meant to share the sameDependencyProperty
across types that are not related (i.e. theTextProperty
ofTextBox
andTextBlock
).