WPF中的Generic.xaml, theme以及custom control

本文介绍了WPF中Generic.xaml的作用,它是控制默认样式的资源字典,当主题风格文件找不到对应样式时,会查找Generic.xaml。对于自定义控件,可以使用DefaultStyleKeyProperty.OverrideMetadata改变默认样式。同时,通过ThemeInfo属性可以指示WPF在外部assembly寻找样式。文章提到了创建自定义Control的实践,并指出在UI库中使用Generic.xaml的好处:减少对外部引用,方便自定义控件获取默认样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做的项目用了个漂亮的开源UI库,结果项目临近结尾发现要支持.Net 3.5, 然而那个UI库却最低支持4.0。欲哭无泪,最后决定拿掉那个库,自己改改style得了。深刻的教训。

作为程序员一般都比较害怕搞界面,这下硬着头皮上,折腾折腾Blend,抄抄改改各种style,弄着弄着居然能看懂个大概了。最后在自己建立的UI库项目中如何组织资源有了一些总结,进入正题:

“Generic.xaml” 早有耳闻,不清楚究竟有什么作用。这次Google之,stackoverflow上早已有人发问。What is so special about Generic.xaml?

第一个答案清晰明了。

Every Control in WPF has a default Style that provides, among other things, the Control's default ControlTemplate. WPF looks for the default style in a special resource dictionary in the Themes folder in the same assembly as the control. The key for the default style is provided by the Control.DefaultStyleKey dependency property, the default value of which is overridden in each sub-class of Control.

The name of the resource dictionary depends on the cu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值