[英]Dependency Property on inherited class ignoring default value
When I write 当我写
public class A : Control
{
static A()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(A), new FrameworkPropertyMetadata(typeof(A)));
}
}
public class B : A
{
}
A
shows its style correctly but B
doesn't have A
's style A
正确显示其样式,但B
没有A
的样式
But if I inherit Button
for example, inherited control displays as button correctly 但是,例如,如果我继承
Button
,则继承的控件将正确显示为button
Why does it work for Button
but not for my control? 为什么它对
Button
但对我的控制无效?
Because you are running that line of code inside the constructor of your class A. When you instantiate class B, constructor of B will run and not A. You could try doing something like this: 因为您正在类A的构造函数中运行该行代码。当实例化类B时,B的构造函数将运行,而不是A。您可以尝试执行以下操作:
public class A : Control
{
static A()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(A), new FrameworkPropertyMetadata(typeof(A)));
}
}
public class B : A
{
static B : base()
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.