原文:
WPF's Style BasedOn
All Style Based
示例Style
1 <Style x:Key="BasedStyle" BasedOn="{x:Null}" TargetType="{x:Type Control}"> 2 <Setter Property="FontFamily" Value="Microsoft YaHei" /> 3 <Setter Property="FontSize" Value="12" /> 4 <Setter Property="Foreground" Value="White" /> 5 <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 6 </Style>
1 <!--引用 BasedStyle--> 2 <ResourceDictionary.MergedDictionaries> 3 <ResourceDictionary Source="BasedStyle.xaml" /> 4 </ResourceDictionary.MergedDictionaries> 5 6 <!--示例控件Style Based代码 以下示例为Button--> 7 <Style x:Key="ButtonBaseBaseStyle" BasedOn="{StaticResource BasedStyle}" TargetType="{x:Type ButtonBase}"> 8 <Setter Property="Height" Value="45" /> 9 <Setter Property="Foreground" Value="{DynamicResource ButtonText}" /> 10 <Setter Property="Padding" Value="0" /> 11 <Setter Property="Margin" Value="0" /> 12 <Setter Property="BorderThickness" Value="1" /> 13 <Setter Property="HorizontalAlignment" Value="Center" /> 14 <Setter Property="VerticalAlignment" Value="Center" /> 15 <Setter Property="HorizontalContentAlignment" Value="Center" /> 16 <Setter Property="VerticalContentAlignment" Value="Center" /> 17 </Style> 18 19 <Style x:Key="ButtonBaseStyle" BasedOn="{StaticResource ButtonBaseBaseStyle}" TargetType="{x:Type Button}"> 20 <Setter Property="Template"> 21 <Setter.Value> 22 <ControlTemplate TargetType="{x:Type Button}"> 23 <Grid> 24 <Border x:Name="border" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" /> 25 <ContentPresenter Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="true" /> 26 27 <Rectangle x:Name="EnabledLayer" Fill="#4CFFFFFF" Visibility="Collapsed" /> 28 </Grid> 29 <ControlTemplate.Triggers> 30 <Trigger Property="IsMouseOver" Value="true"> 31 <Setter Property="Background" Value="{DynamicResource ButtonBackgroundHover}" /> 32 </Trigger> 33 <Trigger Property="IsPressed" Value="true"> 34 <Setter Property="Background" Value="{StaticResource ButtonBackgroundPressed}" /> 35 </Trigger> 36 <Trigger Property="IsEnabled" Value="false"> 37 <Setter TargetName="EnabledLayer" Property="Visibility" Value="Visible" /> 38 </Trigger> 39 </ControlTemplate.Triggers> 40 </ControlTemplate> 41 </Setter.Value> 42 </Setter> 43 </Style> 44 45 <!--示例控件扩展 Style--> 46 <Style x:Key="Success" BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}"> 47 <Setter Property="Background" Value="{DynamicResource ButtonBackground-Success}" /> 48 <Setter Property="BorderBrush" Value="{DynamicResource ButtonBorder-Success}" /> 49 <Style.Triggers> 50 <Trigger Property="IsMouseOver" Value="true"> 51 <Setter Property="Background" Value="#449d44" /> 52 <Setter Property="BorderBrush" Value="#398439" /> 53 </Trigger> 54 </Style.Triggers> 55 </Style>
引用
https://msdn.microsoft.com/zh-cn/library/system.windows.style.basedon(v=vs.110).aspx
https://github.com/ptddqr/bootstrap-wpf-style
总结
使用BasedOn的主要原因是涉及同类控件扩展多,例如不同颜色。
再来就是为了统一基础样式,例如字体大小、样式、颜色,外边框以及水平垂直对称的方式。