为什么DefaultStyleKey不更改子类的默认样式? [英] Why doesn't DefaultStyleKey change the default style for my subclasses?
问题描述
我有一个名为 Handle
的基类,从中可以导出几个基类,例如 RectHandle
和 EllipseHandle
。在那些子类中,我尝试覆盖默认样式键以指向 Handle
,但未应用定位到 Handle
的样式。 。我仍然必须直接或通过 BasedOn样式明确指定 RectHandle
或 EllipseHandle
的目标。我缺少什么?
I have a base class called Handle
from which I derive several base classes such as RectHandle
and EllipseHandle
. In those subclasses I have attempted to override the default style key to point to Handle
but a style targeting Handle
is not applied. I still have to explicitly target RectHandle
or EllipseHandle
either directly, or via a 'BasedOn' style. What am I missing?
以下是DefaultStyleKeyProperty的MSDN摘录:
Here's the MSDN excerpt for DefaultStyleKeyProperty:
A控件通常会将此属性的默认值覆盖为其自己的类型,但是在某些情况下,也可以使用主题字典中存在其样式的基本类型。
A control typically overrides the default value of this property to be its own type, but in some cases could also use a base type for which a style in the theme dictionaries exists.
这是我的代码
public abstract class Handle : Shape
{
static Handle()
{
WidthProperty.OverrideMetadata(
typeof(Handle),
new FrameworkPropertyMetadata(10.0));
HeightProperty.OverrideMetadata(
typeof(Handle),
new FrameworkPropertyMetadata(10.0));
FillProperty.OverrideMetadata(
typeof(Handle),
new FrameworkPropertyMetadata(Brushes.Yellow));
StrokeProperty.OverrideMetadata(
typeof(Handle),
new FrameworkPropertyMetadata(Brushes.Gray));
StrokeThicknessProperty.OverrideMetadata(
typeof(Handle),
new FrameworkPropertyMetadata(2.0));
}
}
public class RectHandle : Handle
{
static RectHandle()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(RectHandle),
new FrameworkPropertyMetadata(typeof(Handle)));
}
protected override Geometry DefiningGeometry
{
get
{
var origin = new Point(-RenderSize.Width / 2, -RenderSize.Height / 2);
var rect = new Rect(origin, RenderSize);
return new RectangleGeometry(rect);
}
}
}
public class EllipseHandle : Handle
{
static RectHandle()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(EllipseHandle),
new FrameworkPropertyMetadata(typeof(Handle)));
}
protected override Geometry DefiningGeometry
{
get
{
var origin = new Point(-RenderSize.Width / 2, -RenderSize.Height / 2);
var rect = new Rect(origin, RenderSize);
return new EllipseGeometry(rect);
}
}
}
还有样式...
<Style TargetType="{x:Type annotations:Handle}">
<Setter Property="Stroke" Value="Red" />
</Style>
同样,这不起作用。
我的解决方法是根据第一种创建其他两种样式,但是我认为这是 DefaultStyleKey $的重点。 c $ c>属性。
My work-around is to create two other styles based on the first, but I thought that was the entire point of the DefaultStyleKey
property.
<Style TargetType="{x:Type annotations:Handle}">
<Setter Property="Stroke" Value="Red" />
</Style>
<Style TargetType="{x:Type annotations:RectHandle}"
BasedOn="{StaticResource {x:Type annotations:Handle}}" />
<Style TargetType="{x:Type annotations:EllipseHandle}"
BasedOn="{StaticResource {x:Type annotations:Handle}}" />
推荐答案
我知道了。 DefaultStyleKey
属性 仅引用在主题 或 Generic.xaml中定义的样式。
在主题下。如果我将 Handle
样式移到那里,突然间,它就会起作用。相反,如果我将其复制到本地窗口中(已完成),则不会。有趣的是查找方式不同。
I figured it out. The DefaultStyleKey
property only refers to styles defined in a theme, or in Generic.xaml
under Themes. If I move my Handle
style there, all of a sudden, it works. If I instead copy it local to the window, which I had done, it doesn't. Interesting that the lookups are different.
这篇关于为什么DefaultStyleKey不更改子类的默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!