WPF's Style BasedOn, wpfstylebasedon
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>
All Style Based
1 <! -- Reference BasedStyle --> 2 <ResourceDictionary. MergedDictionaries> 3 <ResourceDictionary Source = "BasedStyle. xaml"/> 4 </ResourceDictionary. MergedDictionaries> 5 6 <! -- Example control Style Based code the following example is Button --> 7 <Style x: Key = "buttonbasestyle" 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 = "verticalignment" 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 = "{custom BorderBrush}" BorderThickness = "{TemplateBinding BorderThickness}"/> 25 <ContentPresenter Margin = "0" HorizontalAlignment = "Center" verticalignment = "Center" RecognizesAccessKey = "true"/> 26 27 <Rectangl E x: Name = "EnabledLayer" Fill = "#4 CFFFFFF" 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 <Trig Ger 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 <! -- Example control extended 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>
Example Style
Reference
Https://msdn.microsoft.com/zh-cn/library/system.windows.style.basedon (v = vs.110). aspx
Https://github.com/ptddqr/bootstrap-wpf-style
Summary
The main reason for using BasedOn is that it involves many extensions of similar controls, such as different colors.
The next step is to unify the basic style, such as the font size, style, color, outer border, and horizontal vertical symmetry.