为什么将 WPF 样式应用于父控件?
我定义了一个自定义 WPF 样式。我希望网格中的任何按钮都是红色的。但如果我定义这种样式,整个网格都是红色的!!为什么?我明确定义了 Button.Background。
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyStyle">
<Setter Property="Button.Background" Value="Red" /> <!-- Only inner buttons -->
</Style>
</Window.Resources>
<Grid Style="{StaticResource MyStyle}">
<Button Content="Go" Margin="29,36,385,239" />
</Grid>
</Window>
I defined a custom WPF Style. I want any button in the Grid is Red. But if I define this style, the whole grid is red!! Why? I explicitly defined Button.Background.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyStyle">
<Setter Property="Button.Background" Value="Red" /> <!-- Only inner buttons -->
</Style>
</Window.Resources>
<Grid Style="{StaticResource MyStyle}">
<Button Content="Go" Margin="29,36,385,239" />
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了实现您想要的目标,我认为您必须在
Style.Resources
中定义内部Style
。这将使Grid
中的所有Button
选择“内部”Style
,除非它们明确使用另一个Style
>由于
Button.Background
不是附加属性(与TextBlock.Foreground
不同),因此Background
将不会应用于 <网格
中的code>按钮。但至于“为什么
网格
会选择背景
”我无法告诉你。对我来说这似乎是一个错误。Button
的 Background 继承自Control
,Grid
的 Background 继承自Control
从Panel
来看,该值不应该由Grid
使用,但我可能会丢失一些东西此外,如果您尝试直接在
Grid
上设置Button.Background
To achieve what you're after I think you're gonna have to define the inner
Style
s withinStyle.Resources
. This will make all theButton
s in theGrid
pick up the "inner"Style
unless they explictly use anotherStyle
Since
Button.Background
isn't an attached property (unlike e.g.TextBlock.Foreground
), theBackground
won't be applied to theButton
s in theGrid
.But as for the "Why does the
Grid
pick up theBackground
" I couldn't tell you. It seems like a bug to me. Background for aButton
is inherited fromControl
and Background for aGrid
is inherited fromPanel
so as far as I can see, that value shouldn't be used by theGrid
but I might be missing somethingAlso, you'll get the following error if you try to set
Button.Background
directly on aGrid
不能将TargetType设置为button,以便该样式仅应用于Button吗?
Can't you set the TargetType to button so that this style is only applied to a Button?
遗憾的是,风格并非如此。如果你有一个已知的子集合,你可以用类似的东西来作弊(丑陋):
当然,这只有在你知道子索引的情况下才有效,而且它非常脆弱。我不确定它是否适合您的情况,因为您说您必须将样式应用到网格,所以我猜测网格内容是动态生成的。
Sadly, styles don't work like that. If you have a known child collection, you can cheat with something like (ugly):
Of course, this only works if you know the children indices, and it's pretty fragile. I'm not sure that it'll work for your case b/c you said that you must apply the style to the grid, so I'm guessing the grid contents are getting dynamically generated.