.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows 7 >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Country List in DropDownList for Window 7 mobile Application

Posted By:Dhananjay Kumar       Posted Date: April 07, 2010    Points: 25    Category: Windows 7    URL: http://www.dotnetspark.com  

Country List in DropDownList for Window 7 mobile Application.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.
 

Objective

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.

CountryDetail.xml



  
    USA
    Washinton DC
    English
    Dollars
  
  
    England
    London 
    English
    Pounds
  
  
    France
    Paris 
    French
    Euro
  
  
    Germany
    Berlin 
    German
    Mark
   
  
    Russia
    Moscow 
    Russian
    Not Availaible
  
  
    Spain
    Madrid 
    Spanish
    Not Availaible
  
  
    Turkey
    Ankara 
    Not Availaible
    Not Availaible
  
  
    Norway
    Oslo 
    Not Availaible
    Not Availaible
  
  
    Canada
    Ottawa 
    English
    Dollars
  
  
    Mexico
    Mexico 
    Spanish
    Peso
  
  
    China
    Bejing 
    Chinnes
    Not Availaible
  
  
    Japan
    Tokyo 
    Jaopanese
    Yen
  
  
    India
    New Delhi 
    Hindi
    Ruppies
  
  
    Australia
    Canberra 
    English
    Dollars
  

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.

Country.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Countries
{
    public class Country
    {

        public string Name { get; set; }
        public string Capital { get; set; }
        public string Language { get; set; }
        public string Currency { get; set; }

    }
}


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,

    MainPage.xaml


    
        
            
            
        
        
            
            
        
        
        
            
            
             
               
             
                
                    
                        
                    
                
             
        
        
            
                
                
                
                
            
            
            
                
                                
            
            
                
                
            
            
                
                
            
          
    
    

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 System.Xml.Linq;
 
So, full code is as below

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq; 

namespace Countries
{
    public partial class MainPage : PhoneApplicationPage
    {
        //IEnumerable countrydt = null; 
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
            cmbName.SelectionChanged += new SelectionChangedEventHandler(cmbName_SelectionChanged);

            cmbName.ItemsSource = GetCountryDetails();
            cmbName.SelectedIndex = 0; 
        }

       

       List  GetCountryDetails()
        {
            XDocument xmlDocument = XDocument.Load("CountriesDetail.xml");
            
          var   countrydt = from r in xmlDocument.Descendants("Country")
                            select new Country
                            {
                                Capital = r.Element("Capital").Value,
                                Currency = r.Element("Currency").Value,
                                Language = r.Element("Language").Value,
                                Name = r.Element("Name").Value,

                            };
          return countrydt.ToList();
          
        }      

       private void cmbName_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {

           string cntName = ((Country)cmbName.SelectedItem).Name.ToString();
           List country = GetCountryDetails();
           foreach (Country c in country)
           {

               if (c.Name == cntName)
               {
                   txtCountryName.Text = c.Name;
                   txtCapital.Text = c.Capital;
                   txtCurrency.Text = c.Currency;
                   txtLanguage.Text = c.Language;
               }
           }

       }


    }
}


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.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Sushil         Company URL: http://www.dotnetspark.com
Posted Date: September 20, 2012

Hello!
I'm getting following errors:

Error: 1 The type or namespace name 'country' could not be found (are you missing a using directive or an assembly reference?)
On line 30 & 51 [List<country> GetCountryDetails()]

Error 2 A namespace cannot directly contain members such as fields or methods
On line 66 [</country></country></country>]

What I've to do to fix these errors.
For your reference I've pasted the entire C# code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace PageTransitions
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
cmbName.SelectionChanged += new SelectionChangedEventHandler(cmbName_SelectionChanged);

cmbName.ItemsSource = GetCountryDetails();
cmbName.SelectedIndex = 0;
}

List<country> GetCountryDetails()
{
XDocument xmlDocument = XDocument.Load("CountriesDetail.xml");

var countrydt = from r in xmlDocument.Descendants("Country")
select new Country
{
Capital = r.Element("Capital").Value,
Currency = r.Element("Currency").Value,
Language = r.Element("Language").Value,
Name = r.Element("Name").Value,

};
return countrydt.ToList();

}

private void cmbName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

string cntName = ((Country)cmbName.SelectedItem).Name.ToString();
List<country> country = GetCountryDetails();
foreach (Country c in country)
{

if (c.Name == cntName)
{
txtCountryName.Text = c.Name;
txtCapital.Text = c.Capital;
txtCurrency.Text = c.Currency;
txtLanguage.Text = c.Language;
}
}

}
}
}</country></country></country>


Thank you...
Author: Brazil55         Company URL: http://www.dotnetspark.com
Posted Date: May 08, 2013

Hello Dhananjay Kumar

Thanks for your nice post, however I do have a problem. I can not save the selected country from the drop down list. Do you have any idea how to do this?

Thanks for your help.
Brazil55

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend