wpf 为 StackPanel 及其内容创建样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20351375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Style for a StackPanel and its Contents
提问by marty bourque
I want to create a style for Stackpanels using a key. This style will be in a global ResourceDictionary for use everywhere in the application. But it should not only set the style of the Stackpanel, but also its content.
我想使用一个键为 Stackpanels 创建一个样式。此样式将位于全局 ResourceDictionary 中,以便在应用程序中的任何地方使用。但它不仅要设置 Stackpanel 的样式,还要设置它的内容。
I want to use the stackpanel like this:
我想像这样使用堆栈面板:
<StackPanel Style="{StaticResource GlobalStackPanelStyle}">
<Button>test</Button> <-- the button style should also be defined due to the Stackpanel style
</StackPanel>
How should the Resourcedictionary look like if for example all the containing buttons should be green?
例如,如果所有包含按钮都应该是绿色的,那么 Resourcedictionary 应该是什么样子的?
回答by Ming Slogar
You would have something like this in your resource dictionary:
你的资源字典里会有这样的东西:
<Style TargetType="StackPanel" x:Key="GlobalStackPanelStyle">
<Style.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Green" />
</Style>
</Style.Resources>
</Style>

