DefaultStyleKey 设置方法的区别
我正在创建一个自定义控件(源自Control
)并希望为该控件定义默认主题。以前我创建的所有自定义控件都
static IntegerUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(IntegerUpDown),
new FrameworkPropertyMetadata(typeof(IntegerUpDown)));
}
与此 assemble
属性一起使用:
[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly,
ResourceDictionaryLocation.SourceAssembly)]
执行此操作的替代方法是(我也在某些控件中注意到) -
public IntegerUpDown()
{
DefaultStyleKey = typeof(IntegerUpDown);
}
我想知道以下优点和缺点这两种方法应该选择哪一种?
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)));
}
with this assembly
attribute:
[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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以观察到第一种方法要求依赖属性框架注册默认样式键。它只执行一次(在静态构造函数中),然后用于
IntegerUpDown
的所有实例。第二种方法是在单独创建 IntegerUpDown 实例时显式分配 Key。他们俩对我来说似乎都不错。MSDN 说...
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 ofIntegerUpDown
is created on its own. They both seem ok to me.MSDN says ...