Saturday, November 7, 2015

Filling a combobox on selection of another combo box

This example code will explains how to fill a combo box on the selection of another combo box and also explains about the MVVM architecture pattern.

Simple way to understand the  MVVM architecture pattern.
Let us split the MVVM as
      M                     V                       VM
  (model)             (View)               (ViewModel)
(model.cs)        (XAML file)          (ViewModel.cs)

For an example let us consider , there are two combo box , in one combo box we are filling list of countries and on selection of first combobox second combobox will be filled with the country specific languages. In addition to it let us have one text box which is going to show the country currency.

Take a look on Designing the classes.
MODEL : We are going to write methods which brings countries lists.
VIEWMODEL: An another class which contains the Property of display member of MODEL class to show on UI (VIEW)
VIEW: XAML binding of VIEWMODEL.

XAML file
<?xml version="1.0" encoding="utf-8"?>  
 <Window  
   x:Class="MvvmCombo.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
   Title="MvvmCombo"  
   Height="417"  
   Width="496">  
   <Grid  
     Name="mGrid"  
     HorizontalAlignment="Left"  
     VerticalAlignment="Top"  
     Width="479"  
     Height="374">  
     <ComboBox        
       ItemsSource="{Binding Countries}"         
       SelectedItem="{Binding SelectedCountry}"  
       DisplayMemberPath="DisplayName"  
       Grid.Column="1"  
       Grid.Row="1"  
       HorizontalAlignment="Left"  
       VerticalAlignment="Top"  
       Margin="10.99999999687,1.5,0,0"  
       Width="208"  
       Height="27"  
       SelectedIndex="0" />  
     <ComboBox  
       ItemsSource="{Binding Lang}"  
       Grid.Column="1"  
       Grid.Row="2"  
       HorizontalAlignment="Left"  
       VerticalAlignment="Top"  
       Margin="10.99999999687,3.50000000000006,0,0"  
       Width="208"  
       Height="26"  
       SelectedIndex="{Binding SelectedIndex}" />  
     <TextBox  
       Name="txtCur"  
       Text="{Binding Currency}"  
       IsReadOnly="True"  
       Height="29"  
       Width="208"  
       Margin="10.99999999687,3.50000000000007,0,0"  
       VerticalAlignment="Top"  
       HorizontalAlignment="Left"  
       Grid.Row="3"  
       Grid.Column="1" />  
     <Label  
       Content="Countries"  
       Grid.Column="0"  
       Grid.Row="1"  
       HorizontalAlignment="Left"  
       VerticalAlignment="Stretch"  
       Margin="8,1.5,0,5.49999999999994"  
       Width="65"  
       Height="27" />  
     <Label  
       Content="Language"  
       Grid.Column="0"  
       Grid.Row="2"  
       HorizontalAlignment="Left"  
       VerticalAlignment="Stretch"  
       Margin="8,5.00000000000006,0,5.49999999999993"  
       Width="65"  
       Height="24.5" />  
     <Label  
       Grid.Column="0"  
       Grid.Row="3"  
       HorizontalAlignment="Left"  
       VerticalAlignment="Stretch"  
       Margin="8,3.50000000000007,0,8.50000000000004"  
       Width="54"  
       Height="29"  
       Content="Currency" />  
     <TextBox  
       Name="txtCurSym"  
       IsReadOnly="True"  
       Text="{Binding CurrencySym}"  
       Grid.Column="1"  
       Grid.Row="3"  
       HorizontalAlignment="Stretch"  
       VerticalAlignment="Top"  
       Margin="251.635641460119,3.50000000000009,94.1004888627257,0"  
       Width="46"  
       Height="30" />  
     <Grid.ColumnDefinitions>  
       <ColumnDefinition  
         Width="0.182179268637068*" />  
       <ColumnDefinition  
         Width="0.817820731362932*" />  
     </Grid.ColumnDefinitions>  
     <Grid.RowDefinitions>  
       <RowDefinition  
         Height="0.0971428571428572*" />  
       <RowDefinition  
         Height="0.106124505217704*" />  
       <RowDefinition  
         Height="0.0992443324937027*" />  
       <RowDefinition  
         Height="0.102439726520331*" />  
       <RowDefinition  
         Height="0.0988269161568912*" />  
       <RowDefinition  
         Height="0.0856423173803527*" />  
       <RowDefinition  
         Height="0.410579345088161*" />  
     </Grid.RowDefinitions>  
   </Grid>  
 </Window>  


MODEL FILE
 using System;  
 using System.Collections;  
 using System.Collections.Generic;  
 using System.Globalization;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace MvvmCombo  
 {  
   class Model  
   {  
     public Model()  
     {  
     }  
     public List<RegionInfo> GetCountries()  
     {  
       RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);  
       List<RegionInfo> countryNames = new List<RegionInfo>();  
       foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))  
       {  
         country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);  
         countryNames.Add(country);          
       }  
       return countryNames.OrderBy(names => names.DisplayName).Distinct().ToList();  
     }  
     public List<CultureInfo> GetCountriesAll()  
     {  
       List<CultureInfo> list = new List<CultureInfo>();  
       foreach (System.Globalization.CultureInfo ci in   
         System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures))  
       {         
         list.Add(System.Globalization.CultureInfo.CreateSpecificCulture(ci.Name));  
       }  
       return list;  
     }  
   }  
 }  
\
ViewModel File
using System;  
 using System.Collections.Generic;  
 using System.Globalization;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 using System.ComponentModel;  
 using System.Collections.ObjectModel;  
 namespace MvvmCombo  
 {  
   class ViewModel :INotifyPropertyChanged  
   {  
     public event PropertyChangedEventHandler PropertyChanged;   
     private RegionInfo _ri;  
     private Model _m;  
     private void OnPropertyChanged(string propertyName)  
     {  
       if (PropertyChanged != null)  
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
     }  
     public ViewModel(Model m)  
     {  
       _m = m;  
     }  
     private List<CultureInfo> _allCul;  
     public List<RegionInfo> Countries  
     {  
       get  
       {  
         _allCul = _m.GetCountriesAll();  
         return _m.GetCountries(); ;  
       }  
     }  
     private List<string> _lang;  
     public List<string> Lang  
     {  
       get  
       {  
         return _lang;  
       }  
     }  
     public List<string> FillLanguage()  
     {  
       return _lang =  
         _allCul.Where(c => c.EnglishName.Contains(_ri.EnglishName))  
         .Select(c=>c.EnglishName.IndexOf("(")>-1?c.EnglishName.Substring(0,c.EnglishName.IndexOf("(")):  
           c.EnglishName).Distinct().ToList();  
     }  
     private string _currencySym;  
     public string CurrencySym  
     {  
       get  
       {  
         return _currencySym;  
       }  
       set  
       {  
       }  
     }  
     public object SelectedCountry  
     {  
       set  
       {  
         _ri = value as RegionInfo;  
         FillLanguage();          
         _currencySym = _ri.CurrencySymbol;  
         OnPropertyChanged("Lang");  
         OnPropertyChanged("CurrencySym");  
       }  
     }  
   }  
 }  

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code