Data Binding in WPF Windows Application

To have WPF on your machine you should download the .net 3.0 extension for visual studio 2005
 
You can download it from here
 
 
Let us start with creating new WPF windows application.
 
Go to file >> New Project >> Windows Application (WPF)
 
Once the application is created it will give you some default files as below
  • app.config
  • app.xaml
  • window1.xaml
Here we will be dealing with the "window1.xaml" file, idea is to bind Listbox with the database. I am using the MS-Access database.
 
The table structure is as:
 
empId                  autonumber
empName            text
 
"window1.xaml" code is as below
  1. <Window x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="277" Width="356">  
  2.      <ListBox Width="200" Margin="10" ItemsSource="{Binding Path=emp}" Name="lstEmployee">  
  3.           <ListBox.ItemTemplate>  
  4.                <DataTemplate>  
  5.                     <StackPanel>  
  6.                          <TextBlock Text="{Binding Path=empId}" />  
  7.                          <TextBlock Text="{Binding Path=empName}" />  
  8.                     </StackPanel>  
  9.                </DataTemplate>  
  10.           </ListBox.ItemTemplate>  
  11.      </ListBox>  
  12. </Window> 
Code-behind file is as:
 
"window1.xaml.cs":
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows;  
  5. using System.Windows.Controls;  
  6. using System.Windows.Data;  
  7. using System.Data;  
  8. using System.Data.OleDb;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Shapes;  
  14.   
  15. namespace WindowsApplication1 {  
  16.     /// <summary>  
  17.     /// Interaction logic for Window1.xaml  
  18.     /// </summary>  
  19.   
  20.     public partial class Window1: System.Windows.Window {  
  21.         public OleDbConnection oleCon;  
  22.         public OleDbCommand oleComd;  
  23.         string strSql = "SELECT * FROM emp";  
  24.   
  25.         public Window1() {  
  26.             InitializeComponent();  
  27.             BindData();  
  28.         }  
  29.   
  30.         public void viewButton_Click(object sender, RoutedEventArgs args) {  
  31.             //string strVal = peopleListBox.SelectedValue.ToString();  
  32.             //MessageBox.Show(strVal);  
  33.         }  
  34.   
  35.         public void BindData() {  
  36.             oleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDb.mdb");  
  37.             oleComd = new OleDbCommand(strSql, oleCon);  
  38.             DataSet dtst = new DataSet();  
  39.             OleDbDataAdapter adpt = new OleDbDataAdapter();  
  40.             try {  
  41.                 oleCon.Open();  
  42.                 adpt.SelectCommand = oleComd;  
  43.                 adpt.Fill(dtst, "emp");  
  44.                 lstEmployee.DataContext = dtst;  
  45.             } catch (Exception ex) {  
  46.                 //lblMessage.Content = ex.Message;  
  47.             } finally {  
  48.                 oleCon.Close();  
  49.             }  
  50.         }  
  51.     }  
The Binding in the Listbox simply instructs the binding to get the data from the DataContext of the parent (in this case, it walks up the control tree until it finds a DataContext in the Window)
 
To show the employee names in the Listbox, we create bindings in the ItemsTemplate to show the FirstName from the Dataset.
  1. <ListBox.ItemTemplate>  
  2.      <DataTemplate>  
  3.           <StackPanel>  
  4.                <TextBlock Text="{Binding Path=empId}" />  
  5.                <TextBlock Text="{Binding Path=empName}" />  
  6.           </StackPanel>  
  7.      </DataTemplate>  
  8. </ListBox.ItemTemplate> 
Next, we add text boxes to hold our name like this
  1. <TextBlock Text="{Binding Path=empName}" />