Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

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");  
       }  
     }  
   }  
 }  

Thursday, May 7, 2015

DataBinding WPF


Here we look an example of simple data binding in WPF. In this example I use a class for Data Binding. Here we look at the program.

Step1: First we create a Grid in our project:
<Grid x:Name="StuInfo">
     <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" MinWidth="77"></ColumnDefinition>
          <ColumnDefinition></ColumnDefinition>
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
          <RowDefinition></RowDefinition>
     </Grid.RowDefinitions>
</Grid>

Step2: After that we create two TextBlocks and Two TextBoxes in our program and we also create a Button (Next) to see the Next Record according to the program.
 
<TextBlock Text="First Name" Margin="10"></TextBlock>
<TextBlock Text="Last Name" Margin="10" Grid.Row="1"></TextBlock>
<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>
<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1"  Grid.Row="1"></TextBox>
<Button HorizontalAlignment="Left" Margin="0,12,0,9" Name="button1" Width="75"Grid.Column="1" Grid.Row="2">Next</Button>

1.png

Step3: Now we add a Loaded Event handler in the .cs page. This event will be called when the client wants to load the application:
 
public partial class Window1 : Window
{
   
public Window1()
   {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
   }
   void Page_Loaded(object sender, RoutedEventArgs e)
   {

   }
}

Step4: Now we add a class to our program:
public class Stu{
    public string fname { getset; }
    public string lname { getset; }
}  


Step5: Now we write the following code in the Loded Event Handler:
 
void Page_Loaded(object sender, RoutedEventArgs e)
{
   Stu s = new Stu();
    {
      s.fname = "Viji";
      s.lname = "Karg";
    };
      this.StuInfo.DataContext = s;
}

As we mention Binding in TextBox in .xaml page :
 
<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>
<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1"  Grid.Row="1"></TextBox>



Step6: Now we add another Event Handler, which is for the Next Button:
 
public Window1()
{
   InitializeComponent();
   this.Loaded += new RoutedEventHandler(Page_Loaded);
   this.button1.Click += new RoutedEventHandler(button1_Click);
}
void
 button1_Click(object sender, RoutedEventArgs e)
{
   Stu s = new Stu();
    {
       s.fname = "Sid";
       s.lname = "hari";
    };
   this.StuInfo.DataContext = s;
}

Here we add another data in our program. When we click on the Next Button, the output will be:

Tuesday, April 23, 2013

WPF Has Accumulated Multiple WPF XML Namespaces over Time



WPF Has Accumulated Multiple WPF XML Namespaces over Time It’s practically a given that real-world WPF XAML will choose to use the WPF XML namespace as the default namespace, but it turns out that more than one XML namespace is mapped to the main WPF types in the various System.Windows namespaces. WPF 3.0 shipped with support for http://schemas.microsoft.com/winfx/2006/xaml/presentation, but WPF 3.5 defined a new XML namespace— 
http://schemas.microsoft.com/netfx/2007/xaml/presentation—mapped to the same WPF types. (WinFX was the original name for a set of technologies introduced in the .NET Framework 3.0, including WPF,WCF, and WF. That term was abandoned, hence the change in namespace.) WPF 4 has once again defined a new XML namespace that is mapped to the same WPF types:http://schemas.microsoft.com/netfx/2009/xaml/presentation.Despite all these options, it is best to stick with the original http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace because it works in all versions of WPF.(Whether your content works with all versions of WPF is another story, as to do so it must stick to features present only in WPF 3.0.) Note that Silverlight also supports the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace to make it easier to use XAML meant for WPF inside a Silverlight project, although it also defines its own alternative namespace, http://schemas.microsoft.com/client/2007, which is not supported by WPF. The XML namespaces are confusing. They are not schemas. They do not represent a closed set of types that were available when the namespace was introduced. Instead, each version of WPF retrofits all previous namespaces with any new assembly/namespace pairs introduced in the new version. Therefore, the winfx/2006 namespace effectively means “version 3.0 or later,” the netfx/2007 namespace means “version 3.5 or later,” and so on. However,WPF 4 accidentally excludes some namespace/assembly pairs from the netfx/2009 namespace,which makes using omitted types (like TextOptions) pretty challenging! When loose XAML is loaded into Internet Explorer, it is loaded by PresentationHost.exe, which decides which version of the .NET Framework to load based on the XML namespaces on the root element. If the netfx/2009 namespace is present it will load version 4.0, otherwise it will load whichever 3.x version is present.


at Page.28

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