Universal Windows Platform Controls - Part 1

Introduction

In this article, I will discuss about some basic controls of Universal Windows Platform, and XAML is the main design language of Universal Windows Platform. I think it will help you to understand the basic principle of XAML and how it works, its attribute and uses. So let’s get crack it, the essential XAML controls for Universal Windows Platform.

XAML makes your life much easier. Back then, you have to design a lot of pages with same alignment and it is hectic & frustrating, also not that easy task. But XAML brings you the flexibility to make your design portable and easy to arrange. You can copy paste your design and reuse wherever you want. It’s not all, you can shape your design whatever you like to do, and the power is now in your hand. Now let’s see what kind of simple controls you can use in your Universal Windows Platform Application.

Creating A New Project

First of all, we will create a project. Open Visual Studio, and open a “New Project”. Select “Blank App” and name it “Controls1”.

Blank App
                                                                     Figure 1

Click OK and you can see Visual Studio like the following.

Working with HyperlinkButton

Previously, we  worked with simple Button controls in our “Hello world” application. Now, we’ll work with another Button like control called “HyperLinkButton”. It’s used to link a Url or some other things you like.

To do that, you can see Toolbox in the left side of the Visual Studio and you can find it in “All XAML Controls” section. Click on this control and drag it to your design. Like this,

Working with HyperlinkButton
                              Figure 2

Now, to make sure it works, we need another TextBlock control. To do so, drag it from the Toolbox and put it below the Hyperlink button. The designing code is given below.

  1. <!--Hyperlink Button-->  
  2. <HyperlinkButton x:Name="HyperlinkButton_1"   
  3.         Content="HyperlinkButton"   
  4.         HorizontalAlignment="Left"  
  5.         VerticalAlignment="Top"  
  6.         Margin="10,10,0,0"  
  7.         Click="HyperlinkButton_1_Click"  
  8.         Grid.Column="0"  
  9.         Grid.Row="0"  
  10.         Width="114"  
  11.         Height="50"/>  
  12.   
  13. TextBlock x:Name="HB_TextBlock"  
  14. HorizontalAlignment="Left"  
  15. VerticalAlignment="Top"  
  16. TextWrapping="Wrap"                    
  17. Height="50"  
  18. Width="124"  
  19. FontSize="24"  
  20. Grid.Column="1"  
  21. Grid.Row="0"  
  22. Margin="10"/>

                                                      Listing 1

Here, in HyperlinkButton we have an event controller named HyperlinkButton_1_Click, it creates a code block which will handle the background task, when you will click in the hyper link Button and we will show a confirmation message in the TextBlock named HB_TextBlock.

Making Grid

We have made some Grids in our main Grid to arrange the controls in these Grids like Grid 1, 2 and so on. You can make Grids wherever you want, you can customize the Grid as your needs.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.     <Grid.RowDefinitions>  
  3.         <RowDefinition Height="70*"/>  
  4.         <RowDefinition Height="80*"/>  
  5.         <RowDefinition Height="68*"/>  
  6.         <RowDefinition Height="93*"/>  
  7.         <RowDefinition Height="145*"/>  
  8.         <RowDefinition Height="124*"/>  
  9.         <RowDefinition Height="60*"/>  
  10.     </Grid.RowDefinitions>  
  11.     <Grid.ColumnDefinitions>  
  12.         <ColumnDefinition Width="60*"/>  
  13.         <ColumnDefinition Width="40*"/>  
  14.     </Grid.ColumnDefinitions>  
  15. </Grid>

                                                               Listing 2

Now, the main Grid will look like the following:

main Grid
            Figure 3

You can define Row Definitions, number of rows along with their width and column definitions according to their width. We have seven rows and two columns here.

Now, open MainPage.xaml.cs and put the code below the constructor.

  1. private void HyperlinkButton_1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    HB_TextBlock.Text = "OK";  
  4. }

                                                      Listing 3

Now run the application, and it will look like the following picture, after you will click in the HyperlinkButton.

HyperlinkButton
                                                   Figure 4

Working with RadioButton

Well if you can do that, then you are on move. Now, we take another control name RadioButton, and drag it from the TextBlock and put it in another Grid and also a TextBlock in Row 1. The customized code will look like this, or you can simply drag a control and test separately, it’s up to you. I suggest you to do as like I do.

So, our design will look like the following:
Working with RadioButton
                        Figure 5

And the designing code is below.

  1. <RadioButton x:Name="RadioButton_1"  
  2.           Content="RadioButton"  
  3.           HorizontalAlignment="Left"  
  4.           Margin="10,4,0,0"  
  5.           Grid.Row="1"  
  6.           Grid.Column="0"  
  7.           VerticalAlignment="Top"  
  8.           Checked="RadioButton_1_Checked" Height="66" Width="114"/>  
  9.   
  10. <TextBlock x:Name="RB_TextBlock"  
  11.       HorizontalAlignment="Left"  
  12.       VerticalAlignment="Top"  
  13.       Margin="10,10,0,0"  
  14.       TextWrapping="Wrap"                    
  15.       Height="60"  
  16.       Width="124"  
  17.       FontSize="24"  
  18.       Grid.Column="1"  
  19.       Grid.Row="1"/>

                                                         Listing 4

Here, like HyperlinkButton, in our RadioButton we also have an event handler named RadioButton_1_Checked, and in our event handler we will show the confirmation message whether it’s checked or unchecked.

  1. private void RadioButton_1_Checked(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (RadioButton_1.IsChecked == true)  
  4.     {  
  5.         RB_TextBlock.Text = "Checked";  
  6.     }  
  7.     else  
  8.     {                 
  9.         RB_TextBlock.Text = "Not checked";  
  10.     }  
  11. }

                                                            Listing 5

Here, we’re checking whether our RadioButton is checked or not, if it’s checked (true), the TextBlock will show “Checked” or if it’s unchecked (false), the TextBox will show “Not checked”.

After you run your application, it’ll look exactly like this.

RadioButton
                                             Figure 6

Working With TextBlock

Another control, we rapidly use in our every application is TextBlock. We’ve used it in our previous controls also. We will show static data in our TextBlock e.x., “Hello world”.

The design will look like this.

Working with TextBlock
                             Figure 7

Designing code is below.

  1. <!--Text Block-->  
  2. <TextBlock Text="Hello world"  
  3.      HorizontalAlignment="Left"  
  4.      Margin="10,10,0,0"  
  5.      Grid.Row="2"  
  6.      TextWrapping="Wrap"  
  7.      VerticalAlignment="Top"  
  8.      Height="40"  
  9.      Width="380"  
  10.      FontSize="24" Grid.ColumnSpan="2"/>

                                                         Listing 6

We don’t need any Button or event handler in this case, because the text is given statically in the design (Text=”Hello world”).

After you run your application, it’ll look exactly like this.

After you run your application
                                             Figure 8

Working with ToggleSwitch

Another control, we’ll talk about is ToggleSwitch. It’s really a beautiful control that will make your application cooler than before. I think you know, how use a control now, we have done it before. So, just take this control and another TextBlock, and the design will look like this.

tonggle switch
                              Figure 9

The designing code is below,

  1. <!--Toggle Switch-->  
  2. <ToggleSwitch x:Name="ToggleSwitch_1"  
  3.         Header="ToggleSwitch"  
  4.         Margin="10,10,0,0"  
  5.         Grid.Row="3"  
  6.         VerticalAlignment="Top"  
  7.         Toggled="ToggleSwitch_1_Toggled"  
  8.         Width="196"  
  9.         Height="73"/>  
  10.   
  11. t;TextBlock x:Name="TS_TextBlock"  
  12.     HorizontalAlignment="Left"  
  13.     VerticalAlignment="Top"  
  14.     Margin="10,10,0,0"  
  15.     TextWrapping="Wrap"                    
  16.     Height="73"  
  17.     Width="124"  
  18.     FontSize="24"  
  19.     Grid.Column="1"  
  20.     Grid.Row="3"/>

                                                      Listing 7

We have an event handler here, so the C# code is given below.

  1. private void ToggleSwitch_1_Toggled(object sender, RoutedEventArgs e)  
  2. {  
  3.     if (ToggleSwitch_1.IsOn == true)  
  4.     {  
  5.         TS_TextBlock.Text = "This is On";  
  6.     }  
  7.     else  
  8.     {  
  9.         TS_TextBlock.Text = "This is Off";  
  10.     }  
  11. }

                                                  Listing 8

We did the same logic here like the RadioButton.

After you run your application, it’ll look exactly like this.

run your application
                                                Figure 10

Working With ListBox

Our fifth control will be ListBox, its data binding control. It is an important control which has some complicated structure. So let’s see how, we can use it in our application.

Like other controls drag it from the Toolbox and put in the Grid. Here, we need a Button and TextBlock controls.

The design will look like this,

design
                              Figure 11

The designing code is given below,

  1. <!--List Box-->  
  2. <ListBox x:Name="ListBox_1"  
  3.          HorizontalAlignment="Left"  
  4.          Height="120"  
  5.          Margin="10,10,0,0"  
  6.          Grid.Row="4"  
  7.          VerticalAlignment="Top"  
  8.          Width="196"  
  9.          ScrollViewer.VerticalScrollBarVisibility="Visible">  
  10.             <ListBoxItem Content="January"/>  
  11.             <ListBoxItem Content="February"/>  
  12.             <ListBoxItem Content="March"/>  
  13.             <ListBoxItem Content="April"/>  
  14.             <ListBoxItem Content="May"/>  
  15.             <ListBoxItem Content="June"/>  
  16.             <ListBoxItem Content="July"/>  
  17.             <ListBoxItem Content="August"/>  
  18.             <ListBoxItem Content="September"/>  
  19.             <ListBoxItem Content="October"/>  
  20.             <ListBoxItem Content="November"/>  
  21.             <ListBoxItem Content="December"/>  
  22. </ListBox>  
  23.   
  24. <Button Content="Ok"  
  25.         x:Name="Ok"  
  26.         Grid.Column="1"  
  27.         HorizontalAlignment="Left"  
  28.         Margin="10,10,0,0"  
  29.         Grid.Row="4"  
  30.         VerticalAlignment="Top"  
  31.         Width="110"  
  32.         Click="Ok_Click"/>  
  33.   
  34. <TextBlock x:Name="LB_TextBlock"  
  35.             HorizontalAlignment="Left"  
  36.             VerticalAlignment="Top"  
  37.             Margin="10,53,0,0"  
  38.             TextWrapping="Wrap"                    
  39.             Height="82"  
  40.             Width="124"  
  41.             FontSize="24"  
  42.             Grid.Column="1"  
  43.             Grid.Row="4"/>

                                                         Listing 9

Here, we have an event handler named “Ok_Click”, and we have binded some month’s name inside the ListBox’s starting and closing tags. TextBlock’s name is LB_TextBlock. So, the C# code will look like the following:

  1. private void Ok_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string[] month = { "January""February""March""April""May""June""July""August""September""October""November""December" };  
  4.     if (ListBox_1.SelectedValue != null)  
  5.     {  
  6.         LB_TextBlock.Text = month[ListBox_1.SelectedIndex];  
  7.     }  
  8.     else  
  9.     {  
  10.         LB_TextBlock.Text = "Select a item from list.";  
  11.     }  
  12. }

                                                            Listing 10

Here, we have created a string Array named month, and the array index’s values are the month’s name. In If decision statement, first we’re checking if the ListBlock is selected or not, if an item is selected we’re matching the SelectedIndex’s value with our array Index’s value, and if no item is selected then an alert message will be shown in the TextBlock.

If we run the application, it will look exactly like this,

hello world
                                                Figure 12

Working With ComboBox

Now, we’ll talk about a similar control and it’s really awesome than ListBox, just works exactly same as ListBox, but it depends on your application which will be more appropriate in case of your needs. It is called ComboBox. Take it from the  ToolBox or you can just write XAML on your own, like or something like that. So, the design will look like this,

ComboBox
                                 Figure 13

The designing code is given below,

  1. <!--Combo Box-->  
  2. <ComboBox x:Name="ComboBox_1"  
  3.           HorizontalAlignment="Left"  
  4.           Margin="10,0.167,0,0"  
  5.           Grid.Row="5"  
  6.           VerticalAlignment="Top"  
  7.           Width="220">  
  8.     <ComboBoxItem Content="January"/>  
  9.     <ComboBoxItem Content="February"/>  
  10.     <ComboBoxItem Content="March"/>  
  11.     <ComboBoxItem Content="April"/>  
  12.     <ComboBoxItem Content="May"/>  
  13.     <ComboBoxItem Content="June"/>  
  14.     <ComboBoxItem Content="July"/>  
  15.     <ComboBoxItem Content="August"/>  
  16.     <ComboBoxItem Content="September"/>  
  17.     <ComboBoxItem Content="October"/>  
  18.     <ComboBoxItem Content="November"/>  
  19.     <ComboBoxItem Content="December"/>  
  20. </ComboBox>  
  21.   
  22. <TextBlock x:Name="CB_TextBlock"  
  23.            HorizontalAlignment="Left"  
  24.            VerticalAlignment="Top"  
  25.            Margin="10,65.167,0,0"  
  26.            TextWrapping="Wrap"                    
  27.            Height="40"  
  28.            Width="380"  
  29.            FontSize="24"  
  30.            Grid.Row="5" Grid.ColumnSpan="2"/>  
  31.   
  32. <Button Content="Ok"  
  33.         x:Name="Ok_1"  
  34.         Grid.Column="1"  
  35.         HorizontalAlignment="Left"  
  36.         Margin="10,0.167,0,0"  
  37.         Grid.Row="5"  
  38.         VerticalAlignment="Top"  
  39.         Width="125"  
  40.         Click="Ok_1_Click"/>

                                                            Listing 11

And the C# code is here.

  1. private void Ok_1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string[] month = { "January""February""March""April""May""June""July""August""September""October""November""December" };  
  4.     if (ComboBox_1.SelectedValue != null)  
  5.     {  
  6.         CB_TextBlock.Text = month[ComboBox_1.SelectedIndex];  
  7.     }  
  8.     else  
  9.     {  
  10.         CB_TextBlock.Text = "Select a item from list.";  
  11.     }  
  12. }

                                                                  Listing 12

If we run the application, it’ll look exactly like this.

application
                                                Figure 14

AddingA User Control

And lastly, we’ll talk about Popup Box with a Button control, and it will show some messages. For this, we need a User Control. Go to the Solution Explorer, and Add > New Item.

add new item
                                                Figure 15

Now you’ve to select User Control and give it a name called “PopupPanel”.

user control
                                                                    Figure 16

Customize the XAML code, mainly the Grid section.

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

                                                                  Listing 13

Here, we’ve Border brush, StacPanel which will bound the TextBlocks and a Button. The design will look like this,

Popup
                                          Figure 17

The C# code of PopupPanel.xaml.cs is given below. It’s mainly the buttons event handler.

  1. private void ClosePopup(object sender, RoutedEventArgs e)  
  2. {  
  3.     Popup hostPopup = this.Parent as Popup;  
  4.     hostPopup.IsOpen = false;  
  5. }

                                                                     Listing 14

We just made our first User Control. It’s really helpful when you need a custom control in your application.

Working With Popup Window

Now, in our MainPage.xaml, we have to take a TextBlock which will have a header message called “Popup Window” and a Button whose content is “Show Popup”. The design will look like this,

Popup Window
                           Figure 18

The designing code is given below,

  1.   <!--Popup Window-->  
  2.   <TextBlock HorizontalAlignment="Left"  
  3.        Text="Popup Winodow"  
  4.        VerticalAlignment="Top"  
  5.        Margin="10,10,0,0"  
  6.        TextWrapping="Wrap"                    
  7.        Height="40"  
  8.        Width="220"  
  9.        FontSize="24"  
  10.        Grid.Row="6"/>  
  11.   
  12. <Button Content="Show Popup"  
  13.     x:Name="PopupButton"  
  14.     Grid.Column="1"  
  15.     HorizontalAlignment="Left"  
  16.     Margin="10,0,0,0"  
  17.     Grid.Row="6"  
  18.     VerticalAlignment="Top"  
  19.     Width="140"  
  20.     Click="PopupButton_Click"/>

                                                         Listing 15

Our event handler C# code behind is also given here,

  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 16

Here, we have created new object of Popup window, and checked it in our event handler code block by If decision statement. We have created a Popup Child object and set its position and make IsOpen equal to true, so that it shows up when called.

If we run the application, it’ll look exactly like this.

run the application
                                          Figure 19

Finalizing and running our Control Application.

In the end, our full design will look like the following image:

full design
                        Figure 20

And if we run the complete application, it’ll look exactly like this.

complete application
                                          Figure 21

Summary

Well, today we have talked about seven different controls and their uses. Hopefully, it’ll give you a little idea of how to use controls and modify with XAML in Universal Windows Platform Application. I think, I can help you just a little to move on with XAML. Hope you like it. Next time, I’ll be back with other controls. Till then, good bye. Have a nice day. Happy coding.

Download the full source code.

Up Next
    Ebook Download
    View all
    Learn
    View all