将 WPF 样式应用于多个控件
这个问题可能是重复的,但我找不到它。
如果我有一个容器 Window
、StackPanel
、Grid
等,有什么方法可以应用 Style
包含在其中的某种类型的所有控件?
我可以通过使用 Container.Resources 并对 TargetType
设置单独的更改来应用属性更改,但是当我尝试设置目标的 Style
时,我收到错误,告诉我无法设置 Style
。
有没有办法在 XAML 中做到这一点?
This question is probably a duplicate, but I couldn't find it on SO.
If I have a container Window
, StackPanel
, Grid
, etc. is there any way I can apply a Style
to all the controls of a certain type, that are contained within it?
I can apply property changes, by using Container.Resources
and setting individual changes to a TargetType
, but when I tried setting the Style
of the target, I get an error, telling me I can't set Style
.
Is there any way to do this in XAML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点,取决于您要设置的内容。如果属性是公共基类的属性,那么可以。在 WPF 中,您还拥有比 Silverlight 更多的选项,因为您可以继承样式。例如...
通用样式
CommonStyle
将由 3 个隐式样式继承。但您只能指定所有 FrameworkElement 类共有的属性。您无法在 CommonStyle 中设置背景,因为 FrameworkElement 不提供背景属性。因此,尽管 Grid 和 StackPanel 具有背景(继承自 Panel),但它与 Button 具有的背景属性(继承自 Control)并不相同。希望这可以帮助您上路。
Sort of, depending on what you are trying to set. If the properties are properties of a common base class then yes, you can. You also have more options in WPF than Silverlight because you can inherit styles. For example...
The common style,
CommonStyle
would be inherited by the 3 implicit styles. But you can only specify properties that are common to all FrameworkElement classes. You couldn't set Background in CommonStyle because FrameworkElement does not provide a Background property. So even though Grid and StackPanel have Background (inherited from Panel) it is not the same Background property that Button has (inherited from Control.)Hope this helps get you on your way.