【发布时间】:2020-01-29 20:49:09
【问题描述】:
假设我有一个用于容器元素的 WPF 样式,例如自动将样式应用于其子项的网格,如下所示:
<Window.Resources>
<Style TargetType="Grid" x:Key="FormStyle">
<Style.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="50"/>
</Style>
</Style.Resources>
</Style>
</Window.Resources>
然后我怎样才能在网格本身中覆盖该样式的某些元素?例如,假设我希望一个网格有 FormStyle 但也有一个蓝色标签,就像这样(这不起作用):
<!-- this works fine and Label size = 50 -->
<Grid Style="{StaticResource FormStyle}">
<Label Content="Blah"/>
</Grid>
<!-- But this doesnt, label is blue, but normal font size -->
<Grid Style="{StaticResource FormStyle}">
<Grid.Resources>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</Grid.Resources>
<Label Content="Blah"/>
</Grid>
我希望 BasedOn={StaticResource {x:Type Label}} 引用当前范围内标签的当前活动样式 - 即 FormStyle 中的标签样式。但它显然不是,而是指基本的外部标签样式。
如果我在全球范围内这样做
<Style TargetType="Label">
<Setter Property="FontSize" Value="50"/>
</Style>
然后就没事了。
我当然可以只命名样式,但肯定有更简单/不那么冗长的方式吗?
谢谢
【问题讨论】: