I am very weak in WPF and often write a mess. Here I would like to thank nonocast, Tommy, and joans for their help. Thank you for your guidance and help at work, so that I will not take many detours.
It has been a long time since WPF programming is not used. Recently, the project needs to be used, so we need to re-learn the forgotten WPF. The previous blog, WPF style, is easy to write. The previous blog is called WPF style (1, now this article is about WPF style (2.
Style is a set of attribute settings. You can set the same style for different instances of the same type. The premise is that this class must be derived from frameworkelement or frameworkcontentelement.
Style is usually declared as a resource in the resources section.
1. Valid range of style
1.1 If the style is declared in application. resources, its effective range is the entire program
View code
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Themes/Style.xaml" /> <ResourceDictionary Source="Themes/Generic.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
1.2 If the style is declared in window. resources, its valid range is the current form.
View code
<Window.Resources> <Style TargetType="Button"> <Setter Property="Width" Value="75"></Setter> <Setter Property="Height" Value="85"></Setter> <EventSetter Event="Click" Handler="Button_Click"></EventSetter> </Style> </Window.Resources>
1.3 if the style is declared in resources of other controls (such as grid), its valid range is the control.
View code
<Grid.Resources> <Style TargetType="Button"> <Setter Property="Width" Value="75"></Setter> <Setter Property="Height" Value="85"></Setter> <EventSetter Event="Click" Handler="Button_Click"></EventSetter> </Style> </Grid.Resources>
Note:
If only targettype = "button" is written in the style, and X: key is not written, the style of the button in the effective range of the style is the default style.
If X: Key = "buttonstyle1" is entered in style, the method for referencing this style is style = "{staticresource buttonstyle1 }"
2. You can use baseon to "inherit" another style.
View code
<Window.Resources> <Style TargetType="Button" x:Key="buttonstyle1"> <Setter Property="Height" Value="60"></Setter> <Setter Property="Width" Value="60"></Setter> </Style><Style TargetType="Button" BasedOn="{StaticResource ResourceKey=buttonstyle1}"/></Window.Resources>
3. targettype
If targettype is not specified for the style, you must add a type to each attribute when writing the style, so that different types can share the same style:
View code
<Style x:Key="Style1"> <Setter Property="Button.Width" Value="80"></Setter> <Setter Property="Button.Height" Value="80"></Setter> <Setter Property="TextBlock.Text" Value="HelloWorld"></Setter> </Style>
Note:Specify style1 to both the button and textblock. The button automatically ignores the text attributes that do not apply to them. The height and width attributes of the two are both valid.
4. C # reference style in code
Tblock. Style = (style) findresource ("style1 ");
5. events can be bound to styles, for example:
<Style targettype = "button">
<Eventsetter event = "click" handler = "button_click"> </eventsetter>
</Style>
6. Put triggers in the next article.