Apply Styles to a control
Let's say you have created some button controls on a Form. It may be difficult to set styles to every control. To rescue this type of problem, we can set Styles to all controls by setting <styles> element.
Let's create buttons as follows:
Style attribute:
TargetType:
This is used to set Styles to a WPF controls as follows:
TagetType="Button" TargetType="Label" TargetType="ListBoxItem"
e.g.:
- <Style TargetType="Button">
- </Style>
Setting Style Properties to a control: This can be done by using property and value attributes.
e.g.: - <Setter Property="FontWeight" Value="Bold"></Setter> Styles have to set in
- <Window.Resources> </Window.Resources> Element.
- <Window x:Class="WpfComboBox.ButtonStyles" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ButtonStyles" Height="300" Width="500">
- <Window.Resources>
- <Style TargetType="Button">
- <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Setter Property="Background" Value="LightSteelBlue"></Setter> <Setter Property="BorderBrush" Value="Pink"></Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <StackPanel>
- <StackPanel Orientation="Horizontal" Background="Bisque">
- <Button Content="Submit" Width="90" Height="35" Margin="20"></Button>
- <ButtonContent="Cancel" Width="90" Height="35">
- </Button>
- </StackPanel>
- </StackPanel>
- </Grid>
- </Window>
When you run the Program the Applied styles to a Button control looks as in the following image:
Thanks for reading my article.
Shakeer