Country Application for Window 7 mobile


In this article, I am going to show you a Country Application. User will select country from drop down and details of selected country will be displayed.

Expected Output


We will achieve this in three steps

  1. Create XML file for country and corresponding details. And create entity class.
  2. Design phone page
  3. Write code behind to handle selection change event and query XML file using LINQ

Very first create Windows Phone Application. From Silverlight for Windows Phone tab select Windows Phone Application project type.


Creating entity class and XML file as Data source

Right click and add a new item in the project. Select XML file. Then copy paste the below code in that XML file. This XML file contains details of countries. Give a proper name to XML file. Name I am giving here is CountryDetail.XML.


<?xml version=1.0encoding=utf-8 ?> <Countries>

 <Capital>Washinton DC</Capital>



<Capital>London </Capital>






<Capital>Paris </Capital>






<Capital>Berlin </Capital>






<Capital>Moscow </Capital>


<Currency>Not Availaible</Currency>




<Capital>Madrid </Capital>


<Currency>Not Availaible</Currency>




<Capital>Ankara </Capital>

<Language>Not Availaible</Language>

<Currency>Not Availaible</Currency>




<Capital>Oslo </Capital>

<Language>Not Availaible</Language>

<Currency>Not Availaible</Currency>




<Capital>Ottawa </Capital>






<Capital>Mexico </Capital>






<Capital>Bejing </Capital>


<Currency>Not Availaible</Currency>




<Capital>Tokyo </Capital>






<Capital>New Delhi </Capital>






<Capital>Canberra </Capital>






 Now we will create an entity class corresponding to data source XML file. Right click and Add new class in the project. I am giving name of the class as Country.


    1 using System;

    2 using System.Net;

    3 using System.Windows;

    4 using System.Windows.Controls;

    5 using System.Windows.Documents;

    6 using System.Windows.Ink;

    7 using System.Windows.Input;

    8 using System.Windows.Media;

    9 using System.Windows.Media.Animation;

   10 using System.Windows.Shapes;


   12 namespace Countries

   13 {

   14     public class Country

   15     {


   17         public string Name { get; set; }

   18         public string Capital { get; set; }

   19         public string Language { get; set; }

   20         public string Currency { get; set; }


   22     }

   23 }

Design Phone Page


  1. Divide content grid in four rows
  2. In first row put a combo box. Inside combo box put an Item Template and a Data template. Inside Data template put a text box. And Bind this text box with the Name property of Country entity class.


  3. In second, third and fourth row put a stack panel with orientation horizontal. Put one text block and one text box. Text box will display corresponding details.

    Full XAML for design is as below,








mc:Ignorable=”d” d:DesignWidth=”480″ d:DesignHeight=”800″

FontFamily=”{StaticResource PhoneFontFamilyNormal}”

FontSize=”{StaticResource PhoneFontSizeNormal}”

Foreground=”{StaticResource PhoneForegroundBrush}”>

<Grid x:Name=”LayoutRoot” Background=”{StaticResource PhoneBackgroundBrush}”>


<RowDefinition Height=”Auto”/>

<RowDefinition Height=”*”/>


<Grid x:Name=”TitleGrid” Grid.Row=”0″>

<TextBlock Text=”Windows 7 phone” x:Name=”textBlockPageTitle” Style=”{StaticResource PhoneTextPageTitle1Style}”/>

<TextBlock Text=”Country App” x:Name=”textBlockListTitle” Style=”{StaticResource PhoneTextPageTitle2Style}”/>


<Grid x:Name=”ContentGrid” Grid.Row=”1″ Background=”{StaticResource PhoneBackgroundBrush}” Margin=”30,0,0,0″>


<RowDefinition Height=”Auto”/>

<RowDefinition Height=”*”/>


<Grid x:Name=”ContentGrid2″>

<ComboBox x:Name=”cmbName” Height=”75″ SelectionChanged=”cmbName_SelectionChanged” Margin=”21,0,10,0″ Foreground=”Black”>



<TextBlock Text=”{Binding Name}” />





<Grid x:Name=”ContentGrid1″ Grid.Row=”1″>


<RowDefinition Height=”Auto” />

<RowDefinition Height=”60″ />

<RowDefinition Height=”60″ />

<RowDefinition Height=”60″ />


<TextBlock x:Name=”txtCountryName” VerticalAlignment=”Top” Grid.Row=”0″ Height=”100″ FontSize=”72″ Text=”INDIA” Margin=”21,30,25,0″
HorizontalAlignment=”Stretch” />

<StackPanel Orientation=”Horizontal” Grid.Row=”1″>

<TextBlock Text=”Capital” Height=”50″ Width=”139″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″

<TextBox x:Name=”txtCapital” Height=”50″ Width=”336″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″
<StackPanel Orientation=”Horizontal” Grid.Row=”2″>
<TextBlock Text=”Language” Height=”50″ Width=”139″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″

<TextBox x:Name=”txtLanguage” Height=”50″ Width=”336″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″


<StackPanel Orientation=”Horizontal” Grid.Row=”3″>

<TextBlock Text=”Currency” Height=”50″ Width=”139″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″

<TextBox x:Name=”txtCurrency” Height=”50″ Width=”336″ VerticalAlignment=”Top” HorizontalAlignment=”Center” FontSize=”26″





Code Behind

Very first we will create a function. This function will query the XML file using LINQ to XML and return IEnumerable list of entity class Country.



This function is using LINQ to XML to query against XML file. Using XDocument class, I am loading the XML file. Then querying xml file using LINQ to XML and saving result in VAR.Then on the selection changed event of combo box calling the above function and checking for the selected country in combo box and binding. Do not forget to add reference of using


So, full code is as below


    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Net;

    5 using System.Windows;

    6 using System.Windows.Controls;

    7 using System.Windows.Documents;

    8 using System.Windows.Input;

    9 using System.Windows.Media;

   10 using System.Windows.Media.Animation;

   11 using System.Windows.Shapes;

   12 using Microsoft.Phone.Controls;

   13 using System.Xml.Linq;


   15 namespace Countries

   16 {

   17     public partial class MainPage : PhoneApplicationPage

   18     {

   19         //IEnumerable<Country> countrydt = null;

   20         public MainPage()

   21         {

   22             InitializeComponent();


   24             SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

   25             cmbName.SelectionChanged += new SelectionChangedEventHandler(cmbName_SelectionChanged);


   27             cmbName.ItemsSource = GetCountryDetails();

   28             cmbName.SelectedIndex = 0;

   29         }




   33        List<Country>  GetCountryDetails()

   34         {

   35             XDocument xmlDocument = XDocument.Load(“CountriesDetail.xml”);


   37           var   countrydt = from r in xmlDocument.Descendants(“Country”)

   38                             select new Country

   39                             {

   40                                 Capital = r.Element(“Capital”).Value,

   41                                 Currency = r.Element(“Currency”).Value,

   42                                 Language = r.Element(“Language”).Value,

   43                                 Name = r.Element(“Name”).Value,


   45                             };

   46           return countrydt.ToList();


   48         }     


   50        private void cmbName_SelectionChanged(object sender, SelectionChangedEventArgs e)

   51        {


   53            string cntName = ((Country)cmbName.SelectedItem).Name.ToString();

   54            List<Country> country = GetCountryDetails();

   55            foreach (Country c in country)

   56            {


   58                if (c.Name == cntName)

   59                {

   60                    txtCountryName.Text = c.Name;

   61                    txtCapital.Text = c.Capital;

   62                    txtCurrency.Text = c.Currency;

   63                    txtLanguage.Text = c.Language;

   64                }

   65            }


   67        }



   70     }

   71 }


Press F5 to run the application, change the country name and get the details



I hope this article was useful. Thanks for reading. Happy coding.

3 responses to “Country Application for Window 7 mobile”

  1. Hi sir,
    The country appp was good. Please provide the video link of the country app…..

  2. Hi ,

    simply superb. please do add lot of simple apps like this for beginners like a series

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at