我有风格:
<Style TargetType="Panel" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type styles:CommonStyles}, ResourceId=ButtonsPanelStyle}">
<Style.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="MinWidth" Value="75"/>
</Style>
</Style.Resources>
<Setter Property="Background" Value="#E0E0E0"/>
<Setter Property="StackPanel.Orientation" Value="Horizontal"/>
<Setter Property="FlowDirection" Value="RightToLeft"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="MinWidth" Value="{Binding RelativeSource={RelativeSource Self}, Path=DesiredSize.Width}"/>
</Style>
及其用法:
<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
Style="{DynamicResource {x:Static styles:CommonStyles.ButtonsPanelStyleKey}}">
<Button Content="Add" Command="{Binding Add}" Grid.RowSpan="2" Height="23" HorizontalAlignment="Right" Name="AddButton" VerticalAlignment="Center" TabIndex="2" >
<Button.Style>
<Style BasedOn="{StaticResource ResourceKey={x:Type Button}}" TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ActionType}" Value="Edit">
<Setter Property="Button.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DockPanel>
应用于按钮的样式不考虑样式资源中定义的样式中设置的边距。为什么呢?