我有以下风格。如下所述,它很有效:
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="0 0 0 0.3cm" />
<Setter Property="Height" Value="Auto" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</StackPanel.Resources>
<TextBlock Text="Hello"/>
<TextBlock Text="World"/>
</StackPanel>
现在出现了问题。我真正想做的是在第三个位置定义这个样式,然后以各种StackPanels(包括它们的子节点)以上面的工作示例的方式使用它,但不是在所有StackPanels中。这就是我尝试过的。它给出了构建错误:
<Window.Resources>
<Style x:Key="TextBlockWithBottomMargin" TargetType="TextBlock">
<Setter Property="Padding" Value="0 0 0 0.3cm" />
<Setter Property="Height" Value="Auto" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<TabControl>
<!-- omitting some XAML here -->
<TabItem Header="Help" >
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style Binding="{StaticResource TextBlockWithBottomMargin}"></Style> <!-- build error on this line -->
</StackPanel.Resources>
<TextBlock Text="Hello"/>
<TextBlock Text="World"/>
</StackPanel>
<!-- lots more xaml here -->
答案 0 :(得分:2)
使用 在一个级别创建样式:
<StackPanel.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource TextBlockWithBottomMargin}">
<Setter .......
</Style>
</StackPanel.Resources>