WPF:如何将属性值继承到所有子控件?
我有包含其他控件的用户控件。我希望如果我为 UserControl 设置前景色,所有子控件都会自动继承它。我对字体样式/大小也有同样的问题。
我可以以某种方式将这些属性设置为自动/继承吗?是否可以在没有循环的情况下设置所有子控件?
I have UserControls containing other controls. I want that if I set a Foreground color for the UserControl, all child controls automatically inherit it. I have the same problem with font style/size.
Can I set these properties somehow to auto/inherit? Is this possible to set all subcontrols without a loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建资源字典来全局定义默认样式。
您还可以引用资源字典或在任何对象中定义样式。
在任何一种情况下,这些样式都将应用于没有显式定义样式的所有子对象...
示例:
You can you create resource dictionaries to define default styles globally.
You can also reference a resource dictionary or define a style in any object.
In either case those styles will apply to all child objects that don't have a style explicitly defined...
Example:
研究使用样式和 BasedOn 设置。
我最近写了一个类似的示例,位于 here 。不幸的是,这个问题与 Silver Lite 有关,所以没有回答这个问题,但我认为它可能会给您一些关于去哪里寻找的想法。
Look into using Styles and BasedOn settings.
I recently wrote an example of something similar located here. Unfortunally the question was related to Silver Lite so didn't answer the question, but I think it may give you some ideas on where to look.