Styles have a set of properties; here I am going to place some of important and frequent used properties.
Setters: Sets the property values
Triggers: Change the style settings
Resources: Gets or Sets a collection of resources you can use
BasedOn: Creates a specialized style that inherits the settings of another style
TargetType: Gets or Sets the Control for which the style is intended
Let's have a basic example of this, shall we?
Copy and paste the below code in your window1.xaml
- <Window.Resources>
- <Style x:Key="wpfStyle1" TargetType="{x:Type TextBlock}">
- <Setter Property="FontFamily" Value="Verdana"/>
- <Setter Property="FontSize" Value="10"/>
- </Style>
- <Style x:Key="wpfStyle2" TargetType="{x:Type TextBlock}">
- <Setter Property="FontFamily" Value="Arial"/>
- <Setter Property="FontSize" Value="16"/>
- </Style>
- </Window.Resources>
- <Grid>
- <TextBlock Margin="26,41,39,0" Style="{StaticResource wpfStyle1}" Height="31" VerticalAlignment="Top">TextBlock with Style1</TextBlock>
- <TextBlock Margin="26,77,39,0" Height="32" VerticalAlignment="Top">TextBlock with no Style</TextBlock>
- <TextBlock Margin="26,113,67,88" Style="{StaticResource wpfStyle2}">TextBlock with Style2</TextBlock>
- </Grid>
The code seen above creates a Style in the <Window.Resources>. That Style targets elements of type TextBlock, and sets their FontFamily and FontSize properties. Notice that the Styles have keys named wpfStyle1, wpfStyle2. The Style property on each TextBlock is then explicitly set to reference that Style.
Conclusion
This article would help you understand the styles in WPF.
Former memberPosted Jul 20, 2015, 10:21 AM
Nice article