ARTICLE

Data Binding in WPF Windows Application

Posted by Munir Shaikh Articles | Visual C# August 23, 2007
In this tutorial I will discuss on how to bind Data with WPF windows application.
Reader Level:

To have WPF on your machine you should download the .net 3.0 extension for visual studio 2005

 

You can download it from here

http://www.microsoft.com/downloads/details.aspx?familyid=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en

 

Let us start with creating new WPF windows application.

 

Go to file >> New Project >> Windows Application (WPF)

 

Once application is created it will give you some default files as below

  • app.config
  • app.xaml
  • window1.xam 

Here we will be dealing with "window1.xaml" file, idea is to bind listbox with database. I am using MS-Access database.

Table structure is as:

empId                  autonumber
empName            text

 

"window1.xaml" code is as below

<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">

    <ListBox Width="200" Margin="10" ItemsSource="{Binding Path=emp}" Name="lstEmployee">

        <ListBox.ItemTemplate>

            <DataTemplate>

                <StackPanel>

                    <TextBlock Text="{Binding Path=empId}" />

                    <TextBlock Text="{Binding Path=empName}" />

                </StackPanel>

            </DataTemplate>

        </ListBox.ItemTemplate>

    </ListBox>

</Window>

 

Code behind file is as:

"window1.xaml.cs":

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Data;

using System.Data.OleDb;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

 

namespace WindowsApplication1

{

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

 

    public partial class Window1 : System.Windows.Window

    {    

        public OleDbConnection oleCon;

        public OleDbCommand oleComd;

        string strSql = "SELECT * FROM emp";

 

        public Window1()

        {

            InitializeComponent();

            BindData();

        }

        public void viewButton_Click(object sender, RoutedEventArgs args)

        {

            //string strVal = peopleListBox.SelectedValue.ToString();

            //MessageBox.Show(strVal);

        }

        public void BindData()

        {           

            oleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDb.mdb");

            oleComd = new OleDbCommand(strSql,oleCon);

            DataSet dtst = new DataSet();

            OleDbDataAdapter adpt = new OleDbDataAdapter();

            try

            {               

                oleCon.Open();

                adpt.SelectCommand = oleComd;

                adpt.Fill(dtst,"emp");

                lstEmployee.DataContext = dtst;               

            }

            catch(Exception ex)

            {

                //lblMessage.Content = ex.Message;

            }

            finally

            {               

                oleCon.Close();

            }

        }

    }

}

 

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.

 

<ListBox.ItemTemplate>

    <DataTemplate>

        <StackPanel>

            <TextBlock Text="{Binding Path=empId}" />

            <TextBlock Text="{Binding Path=empName}" />

        </StackPanel>

    </DataTemplate>

</ListBox.ItemTemplate>

 

Next, we add text boxes to hold our name like this

 

<TextBlock Text="{Binding Path=empName}" /> 

COMMENT USING