<Style x:Key="BasedStyle" BasedOn="{x:Null}" TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Microsoft YaHei" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
All Style Based
<!--引用 BasedStyle-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BasedStyle.xaml" />
</ResourceDictionary.MergedDictionaries> <!--示例控件Style Based代码 以下示例为Button-->
<Style x:Key="ButtonBaseBaseStyle" BasedOn="{StaticResource BasedStyle}" TargetType="{x:Type ButtonBase}">
<Setter Property="Height" Value="45" />
<Setter Property="Foreground" Value="{DynamicResource ButtonText}" />
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style> <Style x:Key="ButtonBaseStyle" BasedOn="{StaticResource ButtonBaseBaseStyle}" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="border" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
<ContentPresenter Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="true" /> <Rectangle x:Name="EnabledLayer" Fill="#4CFFFFFF" Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundHover}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="{StaticResource ButtonBackgroundPressed}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="EnabledLayer" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <!--示例控件扩展 Style-->
<Style x:Key="Success" BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{DynamicResource ButtonBackground-Success}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorder-Success}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#449d44" />
<Setter Property="BorderBrush" Value="#398439" />
</Trigger>
</Style.Triggers>
</Style>
示例Style
引用
https://msdn.microsoft.com/zh-cn/library/system.windows.style.basedon(v=vs.110).aspx
https://github.com/ptddqr/bootstrap-wpf-style
总结
使用BasedOn的主要原因是涉及同类控件扩展多,例如不同颜色。
再来就是为了统一基础样式,例如字体大小、样式、颜色,外边框以及水平垂直对称的方式。