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>