设置DefaultStyleKey的方法之间的差异 [英] Difference between approches for setting DefaultStyleKey
问题描述
我正在创建一个自定义控件(源自 Control
),并希望为该控件定义默认主题。以前,我创建的所有自定义控件都使用
I am creating a custom control(deriving from Control
) and want to define a default theme for the control. Previously all the custom controls I created, I have used
static IntegerUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(IntegerUpDown),
new FrameworkPropertyMetadata(typeof(IntegerUpDown)));
}
具有此组件
属性:
[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly,
ResourceDictionaryLocation.SourceAssembly)]
执行此操作的替代方法是(我在某些控件中也注意到了)-
Alternative approach to do this is (which I have also noticed in some controls) -
public IntegerUpDown()
{
DefaultStyleKey = typeof(IntegerUpDown);
}
我想知道这两种方法的利弊
I would like to know the pros and cons of these two approaches and which one to prefer?
推荐答案
我可以看到,第一种方法要求依赖性属性框架注册默认样式键。它只会执行一次(在静态构造函数中),然后再用于 IntegerUpDown
的所有实例。当单独创建 IntegerUpDown
的实例时,第二种方法显式地分配Key。他们俩都对我很好。
I can observe that the first approach asks the dependency property framework to register a default style key. It does that only once (being in a static constructor) and then onwards it is used for all instances of IntegerUpDown
. Second approach assignes the Key explicitly when an instance of IntegerUpDown
is created on its own. They both seem ok to me.
MSDN表示...
MSDN says ...
元数据可以被覆盖,以便子类可以通过
覆盖属性的元数据来调整DP,而不是完全由
重新实现属性本身。
Metadata can be overriden so that subclasses can tweak a DP by overriding the property’s metadata, instead of completely re-implementing the property itself.
这篇关于设置DefaultStyleKey的方法之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!