我有一个带有“StackPanel 行”的 DockPanel。
我需要每个 StackPanel 行都获得相同的样式,但是,第一个 StackPanel 行应该获得一个附加样式。
在 CSS 中,我会使用 XAML 样式中似乎没有的级联功能来执行此操作。那么是否可以有多种样式,如下面的伪代码所示?在 XAML 样式中如何解决这个常见问题?
<Window x:Class="TestBinding99382.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBinding99382"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="DataSourceCustomer" ObjectType="{x:Type local:Customer}" MethodName="GetCustomer"/>
<Style x:Key="DataRowStyleFirst" TargetType="StackPanel">
<Setter Property="Margin" Value="0 20 0 0"/>
</Style>
<Style x:Key="DataRowStyle" TargetType="StackPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Margin" Value="0 0 0 0"/>
<Setter Property="DataContext" Value="{StaticResource DataSourceCustomer}"/>
<Setter Property="DockPanel.Dock" Value="Top"/>
</Style>
</Window.Resources>
<DockPanel>
<!-- PSEUDO CODE -->
<StackPanel Style="{StaticResource DataRowStyle,DataRowStyleFirst}">
<TextBlock Text="First Name:"/>
<TextBox Text="{Binding Path=FirstName}" Width="200" Margin="3 0 0 0"/>
</StackPanel>
<StackPanel Style="{StaticResource DataRowStyle}">
<TextBlock Text="Last Name:"/>
<TextBox Text="{Binding Path=LastName}" Width="200" Margin="3 0 0 0"/>
</StackPanel>
</DockPanel>
</Window>