XAML:将样式应用于嵌套控件 [英] XAML: Applying styles to nested controls
问题描述
在 WPF、Prism v4 中,我有一个充当区域的工具栏.在这个区域中,我注入了 UserControls,其中包含工具栏的按钮和分隔符.问题是,因为按钮现在位于 UserControl 中,工具栏样式不再适用.
In WPF, Prism v4, I have a ToolBar which acts as a region. In this region I inject UserControls which contains buttons and separators for the toolbar. The problem is that because the buttons now lives in a UserControl, the toolbar styles no longer applies.
我可以通过定义样式来解决这个问题:
I could solve this by defining the styles:
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
但我想避免这种情况,因为我有数百个按钮.所以我的问题是我是否可以从资源字典中为 ToolBar 下的所有按钮定义样式?
But I would like to avoid this because I got hundreds of buttons. So my question is if I can define styles for all buttons under the ToolBar from a resource dictionary?
谢谢!
推荐答案
您可以使用以下内容:
<Style TargetType="Separator" BasedOn="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
<Style TargetType="Button" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" />
如果你把它放在你的 ToolBar 资源中,那么它就会应用于其中的任何按钮.
If you put that in your ToolBar resources, then it will apply to any buttons inside it.
这篇关于XAML:将样式应用于嵌套控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!