我有以下风格。如下所示,效果很好:
<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 -->