ARTICLE

Data Binding in WPF Windows Application

Posted by Munir Shaikh Articles | Visual Basic 2010 April 11, 2008
In this article, I will discuss on how to bind Data with WPF windows application.
 
Reader Level:

Introduction

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

Imports System

Imports System.Collections.Generic

Imports System.Text

Imports System.Windows

Imports System.Windows.Controls

Imports System.Windows.Data

Imports System.Data

Imports System.Data.OleDb

Imports System.Windows.Documents

Imports System.Windows.Input

Imports System.Windows.Media

Imports System.Windows.Media.Imaging

Imports System.Windows.Shapes

 

Namespace WindowsApplication1

    ''' <summary>

    ''' Interaction logic for Window1.xaml

    ''' </summary>

 

    Partial Public Class Window1

        Inherits System.Windows.Window

        Public oleCon As OleDbConnection

        Public oleComd As OleDbCommand

        Private strSql As String = "SELECT * FROM emp"

 

        Public Sub New()

            InitializeComponent()

            BindData()

        End Sub

        Public Sub viewButton_Click(ByVal sender As Object, ByVal args As RoutedEventArgs)

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

            'MessageBox.Show(strVal);

        End Sub

        Public Sub BindData()

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

            oleComd = New OleDbCommand(strSql, oleCon)

            Dim dtst As New DataSet()

            Dim adpt As New OleDbDataAdapter()

            Try

                oleCon.Open()

                adpt.SelectCommand = oleComd

                adpt.Fill(dtst, "emp")

                lstEmployee.DataContext = dtst

                'lblMessage.Content = ex.Message;

            Catch ex As Exception

            Finally

                oleCon.Close()

            End Try

        End Sub

    End Class
End Namespace 

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

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (
http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor