Using Toggle Button in Windows App


In this article we will use a Toggle button control. This is an advanced control introduced in XAML controls with some new properties. As the name implies, it provides the ability to toggle between two operations upon clicking the same button. In this application we are using that same feature in a social networking site to Like and Unlike images posted by the user in their profile. On the first click of the button, it is liked. Suppose there is an image and when a button is clicked it will indicate that you like this image and when you click again on the like button then this would make it unliked.  

In the following we are providing the entire code of the XAML file and the code behind file to create this mini application. 

Step 1 : First, you will create a new Metro Style Application. Let us see the description with images of how you will create it.

  • Open Visual Studio 2012
  • File -> New -> Project
  • Choose Template -> Visual C# -> Metro Style Application
  • Rename the application


Step 2 : In the Solution Explorer there are two files that we will primarily work with; the MainPage.xaml and MainPage.xaml.cs files.


Step 3 : The MainPage.xaml file is as in the following code:

Code :











<Grid x:Name="LayoutRoot" Background="Red">


            <ColumnDefinition Width=".333*"></ColumnDefinition>

            <ColumnDefinition Width=".333*"></ColumnDefinition>



            <RowDefinition Height=".033*"></RowDefinition>

            <RowDefinition Height=".333*"></RowDefinition>

            <RowDefinition Height=".033*"></RowDefinition>

            <RowDefinition Height=".033*"></RowDefinition>



        <TextBlock x:Name="tblVote" Text="Please cast your Vote:" FontSize="40"

                   Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Center"



        <Image Source="Tulips.jpg" Grid.Row="1" Grid.ColumnSpan="2">


        <TextBlock x:Name="tb1" Grid.Row="2" Grid.ColumnSpan="2" FontSize="40"

                   HorizontalAlignment="Center" VerticalAlignment="Center">


        <ToggleButton x:Name="tbtnVote" Checked="tbtnVote_VoteChanged"

                      Unchecked="tbtnVote_VoteChanged" Grid.ColumnSpan="2"

                      Grid.Row="3" Indeterminate="tbtnVote_VoteChanged"

                      IsThreeState="True"  HorizontalAlignment="Center"

                      VerticalAlignment="Center" Content="Like / Dislike">




Step 4 : The MainPage.xaml.cs file is as in the following code:

Code :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App8
public sealed partial class MainPage : Page
public MainPage()
this.tbtnVote.IsChecked = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
private void tbtnVote_VoteChanged(object sender, RoutedEventArgs e)
private void Voting()
switch (this.tbtnVote.IsChecked)
case null:
this.tb1.Text = "--------------";
case true:
this.tb1.Text = "You Unlike this image";
case false:
this.tb1.Text = "You like this image";

Step 5 : After running this code the output looks like this:




Up Next
    Ebook Download
    View all
    View all