Introduction
 
 Welcome again! Today I’ll talk about XAML styling. How you can make your XAML  controls more beautiful and customized. If you search “Universal Windows  Platform XAML style” you’ll get some helpful references. Styling XAML is not  only for customizing your controls but also make code much clean and easily  readable. So, let’s get crack in Windows Phone XAML Styling.
 
 I’ll just try to explain how you can use XAML styling in you existent projects.  I’m just going to modify my existing user control to show you the procedure. If  you’ve read my  Windows Phone Controls – Part 1 article, then you can understand the  difference between previous and current XAML code. I’ll not modify all the  controls, but the “Popup Window”. I have used a “User Control”, I’ll just  modify that page.
 
 Creating a New Project and Add a User Control
 
 Firstly, take a new Project and add a new “User Control”. We’ve used  these XAML code in our previous project.
 
- <Grid>  
-     <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
-         <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
-             <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
-                 <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
-                     <StackPanel Orientation="Vertical" Height="200" Width="200" VerticalAlignment="Center">  
-                         <TextBlock Text="This is a Popup!" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,60,0,0"/>  
-                         <TextBlock Text="Hit the button again to hide me" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" TextWrapping="Wrap" TextAlignment="Center"/>  
-                         <Button HorizontalAlignment="Center" Content="Close Popup" Click="ClosePopup" />  
-                     </StackPanel>  
-                 </Border>  
-             </Border>  
-         </Border>  
-     </Border>  
- </Grid>  
 And the design looks like the following screenshot:  
![design]() Figure 1
                                                       Figure 1  Now, we’ll use XAML styling in the same XAML code and make it much clean and  customized as well. To do so, you’ve to put resources as in the following code: 
- <UserControl  
- ...  
-     d:DesignWidth="400">  
-   
-     <UserControl.Resources>  
-         ...  
-     </UserControl.Resources>  
-   
-     <Grid>  
-         ...  
-     </Grid>  
- </UserControl>  
  All you have to do is put all your style properties inside the Resources tag.  Firstly, we’ll create a “Border Style” for our “Border” control. 
- <UserControl.Resources>  
-     <Style x:Key="BorderStyle" TargetType="Border">  
-         <Setter Property="BorderThickness" Value="2"/>  
-         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
-     </Style>  
- </UserControl.Resources>  
 If you’re using this in Blank of Basic pages, the code will be like  the following: 
- <Page.Resources>  
-     <Style x:Key="BorderStyle" TargetType="Border">  
-         <Setter Property="BorderThickness" Value="2"/>  
-         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
-     </Style>  
- </Page.Resources>  
 As we’re using a “
User Control”, so we used “
UserControl.Resources”. 
 Here, we’re considering only one “
Border” control. If you look in the above code, we gave the style name “
BorderStyle” and target set to “Border”. In  which control you work you have to give a unique name and set target of that  control. Also, we have set a property name “
BorderThickness” and set value  to “2”, which will make the thickness of the borders outer edges. And we have also set “CornerRadious” to “0,10,0,10”, which will make the upper right and  lower left corner edges little bit round.  
![popup]() Figure 2
                                                    Figure 2  Now, similarly we have added “TextBox” and “Button” styles.
- <UserControl.Resources>  
-     <Style x:Key="BorderStyle" TargetType="Border">  
-         <Setter Property="BorderThickness" Value="2"/>  
-         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
-     </Style>  
-     <Style x:Key="StackPanelStyle" TargetType="StackPanel">  
-         <Setter Property="Orientation" Value="Vertical"/>  
-         <Setter Property="VerticalAlignment" Value="Center"/>  
-         <Setter Property="Height" Value="200"/>  
-         <Setter Property="Width" Value="200"/>  
-     </Style>  
-     <Style x:Key="ButtonStyle" TargetType="Button">  
-         <Setter Property="HorizontalAlignment" Value="Center"/>  
-         <Setter Property="Content" Value="Close Popup"/>  
-         <Setter Property="Background" Value="Green"/>  
-     </Style>  
-     <Style x:Key="TextBlockStyle" TargetType="TextBlock">  
-         <Setter Property="VerticalAlignment" Value="Center"/>  
-         <Setter Property="HorizontalAlignment" Value="Center"/>  
-         <Setter Property="Text" Value="This is a Popup!"/>  
-         <Setter Property="Margin" Value="0,60,0,0"/>  
-         <Setter Property="Foreground" Value="Red"/>  
-     </Style>  
-     <Style x:Key="TextBlockStyle1" TargetType="TextBlock">  
-         <Setter Property="VerticalAlignment" Value="Center"/>  
-         <Setter Property="HorizontalAlignment" Value="Center"/>  
-         <Setter Property="TextAlignment" Value="Center"/>  
-         <Setter Property="TextWrapping" Value="Wrap"/>  
-         <Setter Property="Text" Value="Hit the button again to hide me."/>  
-         <Setter Property="Margin" Value="0,10,0,0"/>  
-         <Setter Property="Foreground" Value="Gray"/>  
-     </Style>  
- </UserControl.Resources>  
 If you take a look at the old XAML code, you can see all the properties are here  exactly, but with an exception in “Button” click event. You have to put this event in the main  Grid’s “Button” control code. 
- <Grid>  
-     <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}"  
-             Background="{StaticResource ApplicationPageBackgroundThemeBrush}"  
-             Style="{StaticResource BorderStyle}">  
-         <StackPanel Style="{StaticResource StackPanelStyle}">  
-             <TextBlock Style="{StaticResource TextBlockStyle}"/>  
-             <TextBlock Style="{StaticResource TextBlockStyle1}"/>  
-             <Button Style="{StaticResource ButtonStyle}" Click="ClosePopup" />  
-         </StackPanel>  
-     </Border>  
- </Grid>  
 All you need to do is just reference the styles in corresponding controls. Like  in “Border” control we used “
Style=”{StaticResource BorderStyle}”.. “.  After the “
StaticResource” name the Style name.  
Put Your Styles Separate
  Another important thing you can do is to separate the XAML styling into  different location. To make much clean XAML, just open “App.xaml” and  put the same code there, like this 
- <Application  
- ...  
-     >  
-       
-     <Application.Resources>  
-         <Style x:Key="BorderStyle" TargetType="Border">  
-             <Setter Property="BorderThickness" Value="2"/>  
-             <Setter Property="CornerRadius" Value="5"/>  
-         </Style>  
-         <Style x:Key="StackPanelStyle" TargetType="StackPanel">  
-             <Setter Property="Orientation" Value="Vertical"/>  
-             <Setter Property="VerticalAlignment" Value="Center"/>  
-             <Setter Property="Height" Value="200"/>  
-             <Setter Property="Width" Value="200"/>  
-         </Style>  
-     </Application.Resources>  
- </Application>  
 Only difference is the tag, here it should be “
Application.Resources”,  because it’s in “
App.xaml” file. So, the tag structure should be like “
Type.Resources”.  Here type can be “
Page”, “
Application”, “
UserControl” etc. 
 Now, in “
Main Page.xaml” take Button control to show the “Popup Window”. 
- <Page.Resources>  
-     <Style x:Key="ButtonStyle" TargetType="Button">  
-         <Setter Property="Name" Value="PopupButton"/>  
-         <Setter Property="HorizontalAlignment" Value="Left"/>  
-         <Setter Property="VerticalAlignment" Value="Top"/>  
-         <Setter Property="Width" Value="140"/>  
-         <Setter Property="Margin" Value="10,0,0,0"/>  
-         <Setter Property="Content" Value="Show Popup"/>  
-     </Style>  
- </Page.Resources>  
-   
- <Grid>  
-     <Button Style="{StaticResource ButtonStyle}" Click="PopupButton_Click"/>  
- </Grid>  
  And C# code of this “
Button” event handler is given below. 
- private void PopupButton_Click(object sender, RoutedEventArgs e)  
- {  
-     if (!popup.IsOpen)  
-     {  
-         popup.Child = new PopupPanel();  
-         popup.VerticalOffset = 200.0;  
-         popup.HorizontalOffset = 160.0;  
-         popup.IsOpen = true;  
-     }  
- }  
-    
- Popup popup = new Popup();  
  So, if you run the application it’ll look like the following: 
 Windows Phone Emulator:  
![Windows Phone Emulator]() 
  Windows 10 Local Machine:  
![Windows 10 Local Machine]() 
  Here, we’ve another sample in which “Popup Window” is round. You can simply do  that just changing this code in “PopupPanel.xaml” 
- <Page.Resources>  
-     <Style x:Key="BorderStyle" TargetType="Border">  
-         <Setter Property="BorderThickness" Value="2"/>  
-         <Setter Property="CornerRadius" Value="100"/>  
-     </Style>  
- </Page.Resources>  
 Summary
  Hope, you have understood the basics of XAML styling. I’ll be here with a new topic  soon. Till then good bye. Have a nice day. 
 Download the full source 
code.