Introduction
Let's be honest, the more attractive the UI, the more satisfactory the user experience will be.
The concept of styles in WPF is exactly the same as CSS in web development. Styles are used for formatting purposes.
We can define styles in App.xaml or ResourceDictionaries to reuse them across the application.
Inside a control itself, if the usage scope is single control.
There are 3 ways to define a style:
- In ResourceDictionaries to use application-wide you need to define the x:Key so that caller can access style using that key as a resource.
- In Window.Resources to use them across that screen you need to define x:Key
- Inside a control, itself, applied only on that control: No Need to define x:Key, as style is already defined inside a control which supposes to be styled.
Create a WPF project & add a Resource Dictionary inside your project.
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:A">
- <Thickness x:Key="MarrginTop">0 30 0 0</Thickness>
- <Style x:Key="SubmitButtonStyle"
- BasedOn="{StaticResource {x:Type Button}}"
- TargetType="Button">
- <Setter Property="Height" Value="20"/>
- <Setter Property="Width" Value="100"/>
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Center"/>
- <Setter Property="Content" Value="Submit"/>
- <Setter Property="BorderBrush" Value="Black"/>
- <Setter Property="BorderThickness" Value="2"/>
- <Setter Property="BorderThickness" Value="2"/>
- <Setter Property="Margin" Value="{StaticResource MarrginTop}"/>
- </Style>
- </ResourceDictionary>
Go to MainWindow.xaml and apply created style there! Use style with Style="{StaticResource SubmitButtonStyle}"
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:A"
- mc:Ignorable="d"
- Title="MainWindow" Height="180" Width="300">
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="ResourceDictionaryTemplate.xaml"/>
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Window.Resources>
- <Grid x:Name="MainGrid">
- <Button x:Name="ButtonStyleMe"
- Style="{StaticResource SubmitButtonStyle}"/>
- </Grid>
- </Window>
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="ResourceDictionaryTemplate.xaml"/>
- </ResourceDictionary.MergedDictionaries>
- <Style x:Key="TextboxUserInputStyle"
- BasedOn="{StaticResource {x:Type TextBox}}"
- TargetType="TextBox">
- <Setter Property="Height" Value="20"/>
- <Setter Property="Width" Value="200"/>
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Top"/>
- <Setter Property="BorderBrush" Value="Black"/>
- <Setter Property="BorderThickness" Value="2"/>
- <Setter Property="Margin" Value="0 50 0 0"/>
- </Style>
- </ResourceDictionary>
- </Window.Resources>
Add TextBox where style can be applied.
- <TextBox x:Name="TextBoxUserInput"
- Style="{StaticResource TextboxUserInputStyle}"/>
Last but not least... Inside the control itself let's add a TextBlock which will display whatever user enters in the TextBox.
- <TextBlock x:Name="TextBlockShowUserDetails" Text="{Binding ElementName=TextBoxUserInput, Path=Text}">
- <TextBlock.Style>
- <Style BasedOn="{StaticResource {x:Type TextBlock}}"
- TargetType="TextBlock">
- <Setter Property="FontFamily" Value="Baskerville,Georgia"/>
- <Setter Property="FontStyle" Value="Italic"/>
- <Setter Property="FontWeight" Value="DemiBold"/>
- <Setter Property="Height" Value="20"/>
- <Setter Property="Width" Value="200"/>
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Center"/>
- <Setter Property="Margin" Value="0 100 0 0"/>
- </Style>
- </TextBlock.Style>
- </TextBlock>
Now, your final MainWindow.xaml will look something like this:
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:A"
- mc:Ignorable="d"
- Title="MainWindow" Height="180" Width="300">
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="ResourceDictionaryTemplate.xaml"/>
- </ResourceDictionary.MergedDictionaries>
- <Style x:Key="TextboxUserInputStyle"
- BasedOn="{StaticResource {x:Type TextBox}}"
- TargetType="TextBox">
- <Setter Property="Height" Value="20"/>
- <Setter Property="Width" Value="200"/>
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Top"/>
- <Setter Property="BorderBrush" Value="Black"/>
- <Setter Property="BorderThickness" Value="2"/>
- <Setter Property="Margin" Value="0 50 0 0"/>
- </Style>
- </ResourceDictionary>
- </Window.Resources>
- <Grid x:Name="MainGrid">
- <TextBox x:Name="TextBoxUserInput"
- Style="{StaticResource TextboxUserInputStyle}"/>
- <Button x:Name="ButtonStyleMe"
- Style="{StaticResource SubmitButtonStyle}"/>
- <TextBlock x:Name="TextBlockShowUserDetails" Text="{Binding ElementName=TextBoxUserInput, Path=Text}">
- <TextBlock.Style>
- <Style BasedOn="{StaticResource {x:Type TextBlock}}"
- TargetType="TextBlock">
- <Setter Property="FontFamily" Value="Baskerville,Georgia"/>
- <Setter Property="FontStyle" Value="Italic"/>
- <Setter Property="FontWeight" Value="DemiBold"/>
- <Setter Property="Height" Value="20"/>
- <Setter Property="Width" Value="200"/>
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Center"/>
- <Setter Property="Margin" Value="0 100 0 0"/>
- </Style>
- </TextBlock.Style>
- </TextBlock>
- </Grid>
- </Window>

Now run your WPF application and see Styles are applied on each control as expected.
I hope this article has cleared the confusion you might have had about styling in WPF.
Feel free to apply them to your project to understand how they work.
Thank you for visiting this article.
If you have any doubts, you can connect with me @
Happy Coding!

Join the conversation! Your thoughts help the community grow.