Windows Phone - XAML Styling

Introduction

Welcome again! Today I'll talk about XAML styling. How to make your XAML controls prettier and customized. If you search for “windows phone xaml style” then you'll get some helpful references. Styling XAML is not only for customizing your controls but also making code cleaner and easily readable. So let's get cracking in Windows Phone XAML Styling.

I'll just try to explain how to use XAML styling in you existing 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've used a “User Control”, I'll just modify that page.

Create a New Project and Add a User Control

First create a new project and add a new “User Control”. We've used these XAML code in our previous projects.

  1. <Grid>  
  2.     <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
  3.         <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
  4.             <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
  5.                 <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" BorderThickness="1">  
  6.                     <StackPanel Orientation="Vertical" Height="200" Width="200" VerticalAlignment="Center">  
  7.                         <TextBlock Text="This is a Popup!" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,60,0,0"/>  
  8.                         <TextBlock Text="Hit the button again to hide me" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0" TextWrapping="Wrap" TextAlignment="Center"/>  
  9.                         <Button HorizontalAlignment="Center" Content="Close Popup" Click="ClosePopup" />  
  10.                     </StackPanel>  
  11.                 </Border>  
  12.             </Border>  
  13.         </Border>  
  14.     </Border>  
  15. </Grid>  
Listing 1

And the design looks like this.

popup
                                                                              Figure 1

Now, we'll use XAML styling in the same XAML code and make it much cleaner and customized as well. To do so, you need to use resources as shown below.
  1. <UserControl  
  2. ...  
  3.     d:DesignWidth="400">  
  4.   
  5.     <UserControl.Resources>  
  6.         ...  
  7.     </UserControl.Resources>  
  8.   
  9.     <Grid>  
  10.         ...  
  11.     </Grid>  
  12. </UserControl>  
Listing 2

Creating Styles

All you must do is put all your style properties inside the Resources tag. First of all we'll create a “Border Style” for our “Border” control.
  1. <UserControl.Resources>  
  2.     <Style x:Key="BorderStyle" TargetType="Border">  
  3.         <Setter Property="BorderThickness" Value="2"/>  
  4.         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
  5.     </Style>  
  6. </UserControl.Resources>  
Listing 3

Note: If you're using this in Blank of Basic pages, the code will be like this.
  1. <Page.Resources>  
  2.     <Style x:Key="BorderStyle" TargetType="Border">  
  3.         <Setter Property="BorderThickness" Value="2"/>  
  4.         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
  5.     </Style>  
  6. </Page.Resources>  
Listing 4

Since we're using a “User Control”, we used “UserControl.Resources”.

Here, we're considering only one the “Border” control. If you look at the preceding code, we gave the style name “BorderStyle” and set the target to “Border”. In whatever control you work with, you need to provide a unique name and set the target of that control. Also, we've set a property named “BorderThickness” and set the value to “2”, that will make the thickness of the border's outer edges. And we've also set “CornerRadious” to “0,10,0,10”, that will make the upper right and lower left corner edges a bit round.

popup status
                                                                     Figure 2

Now, similarly we've added “TextBox” and “Button” styles.
  1. <UserControl.Resources>  
  2.     <Style x:Key="BorderStyle" TargetType="Border">  
  3.         <Setter Property="BorderThickness" Value="2"/>  
  4.         <Setter Property="CornerRadius" Value="0,10,0,10"/>  
  5.     </Style>  
  6.     <Style x:Key="StackPanelStyle" TargetType="StackPanel">  
  7.         <Setter Property="Orientation" Value="Vertical"/>  
  8.         <Setter Property="VerticalAlignment" Value="Center"/>  
  9.         <Setter Property="Height" Value="200"/>  
  10.         <Setter Property="Width" Value="200"/>  
  11.     </Style>  
  12.     <Style x:Key="ButtonStyle" TargetType="Button">  
  13.         <Setter Property="HorizontalAlignment" Value="Center"/>  
  14.         <Setter Property="Content" Value="Close Popup"/>  
  15.         <Setter Property="Background" Value="Green"/>  
  16.     </Style>  
  17.     <Style x:Key="TextBlockStyle" TargetType="TextBlock">  
  18.         <Setter Property="VerticalAlignment" Value="Center"/>  
  19.         <Setter Property="HorizontalAlignment" Value="Center"/>  
  20.         <Setter Property="Text" Value="This is a Popup!"/>  
  21.         <Setter Property="Margin" Value="0,60,0,0"/>  
  22.         <Setter Property="Foreground" Value="Red"/>  
  23.     </Style>  
  24.     <Style x:Key="TextBlockStyle1" TargetType="TextBlock">  
  25.         <Setter Property="VerticalAlignment" Value="Center"/>  
  26.         <Setter Property="HorizontalAlignment" Value="Center"/>  
  27.         <Setter Property="TextAlignment" Value="Center"/>  
  28.         <Setter Property="TextWrapping" Value="Wrap"/>  
  29.         <Setter Property="Text" Value="Hit the button again to hide me."/>  
  30.         <Setter Property="Margin" Value="0,10,0,0"/>  
  31.         <Setter Property="Foreground" Value="Gray"/>  
  32.     </Style>  
  33. </UserControl.Resources>  
Listing 5

If you have a look at the old XAML code, you can see all the properties are here exactly, except the “Button” click event. You must put this event in the main Grid's “Button” control code.
  1. <Grid>  
  2.     <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}"  
  3.             Background="{StaticResource ApplicationPageBackgroundThemeBrush}"  
  4.             Style="{StaticResource BorderStyle}">  
  5.         <StackPanel Style="{StaticResource StackPanelStyle}">  
  6.             <TextBlock Style="{StaticResource TextBlockStyle}"/>  
  7.             <TextBlock Style="{StaticResource TextBlockStyle1}"/>  
  8.             <Button Style="{StaticResource ButtonStyle}" Click="ClosePopup" />  
  9.         </StackPanel>  
  10.     </Border>  
  11. </Grid>  
Listing 6

All you need to do is just reference the styles in the corresponding controls. Like in the “Border” control we used “Style=”{StaticResource BorderStyle}”.. “. After the “StaticResource” name is the Style name.

Put Your Styles Separate

Another important thing you can do is to separate the XAML styling into a different location. To make a much cleaner XAML. To do so, just open “App.xaml” and put the same code there, like this:
  1. <Application  
  2. ...  
  3.     >  
  4.     <!--Application Resources-->  
  5.     <Application.Resources>  
  6.         <Style x:Key="BorderStyle" TargetType="Border">  
  7.             <Setter Property="BorderThickness" Value="2"/>  
  8.             <Setter Property="CornerRadius" Value="5"/>  
  9.         </Style>  
  10.         <Style x:Key="StackPanelStyle" TargetType="StackPanel">  
  11.             <Setter Property="Orientation" Value="Vertical"/>  
  12.             <Setter Property="VerticalAlignment" Value="Center"/>  
  13.             <Setter Property="Height" Value="200"/>  
  14.             <Setter Property="Width" Value="200"/>  
  15.         </Style>  
  16.     </Application.Resources>  
  17. </Application>  
Listing 7

The only difference is the tag. Here it should be “Application.Resources”, because it's in the “App.xaml” file. So, the tag structure should be like “Type.Resources”. Here the type can be “Page”, “Application”, “UserControl” and so on.

Now, in “Main Page.xaml” take a Button control to show the “Popup Window”.
  1. <Page.Resources>  
  2.     <Style x:Key="ButtonStyle" TargetType="Button">  
  3.         <Setter Property="Name" Value="PopupButton"/>  
  4.         <Setter Property="HorizontalAlignment" Value="Left"/>  
  5.         <Setter Property="VerticalAlignment" Value="Top"/>  
  6.         <Setter Property="Width" Value="140"/>  
  7.         <Setter Property="Margin" Value="10,0,0,0"/>  
  8.         <Setter Property="Content" Value="Show Popup"/>  
  9.     </Style>  
  10. </Page.Resources>  
  11.   
  12. <Grid>  
  13.     <Button Style="{StaticResource ButtonStyle}" Click="PopupButton_Click"/>  
  14. </Grid>  
Listing 8

Code Behind

And the C# code of this “Button” event handler is given below.
  1. private void PopupButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (!popup.IsOpen)  
  4.     {  
  5.         popup.Child = new PopupPanel();  
  6.         popup.VerticalOffset = 250.0;  
  7.         popup.HorizontalOffset = 100.0;  
  8.         popup.IsOpen = true;  
  9.     }  
  10. }  
  11.    
  12. Popup popup = new Popup();  
Listing 9

Running the Application

So, if you run the application it'll look like this.

show popup

Here, we've another sample with a round “Popup Window”. You can do that by simply changing the following code in “PopupPanel.xaml”.
  1. <Page.Resources>  
  2.     <Style x:Key="BorderStyle" TargetType="Border">  
  3.         <Setter Property="BorderThickness" Value="2"/>  
  4.         <Setter Property="CornerRadius" Value="100"/>  
  5.     </Style>  
  6. </Page.Resources>  
Listing 10

Summary

I hope you've understood the basics of XAML styling. I'll be here with a new topic soon. Until then good bye. Have a nice day.

Happy coding!

Read the original article at http://bit.ly/1omk3Is.

Up Next
    Ebook Download
    View all
    Learn
    View all