Introduction The Windows Presentaion Foundation is new generatuion in desktop system in .NET technoloies.It's completely new way to create cute system using .NET Framework.According this there are in WPF controls little different from the Windows Forms standard controls. Since this article would explain and how to work with ListView control in WPF. Technologies WPF
Language C#
Prerequisite Visual Studio 2008 and .NET Framework 3.5 SP1
Implementation To this we need design a simple GUI using XAML. when finished GUI its look like followings. 
Above Figure simple WPF design using XAML. Here XAML for the above figure. {codecitation class="brush: xml; gutter: true;" width="650px"} <Window x:Class="CG.CS.WPFDemos.wdwListViewDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="wdwListViewDemo" Height="300" Width="600" WindowState="Normal" WindowStyle="SingleBorderWindow" Loaded="Window_Loaded"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="1.5*"></RowDefinition> </Grid.RowDefinitions> <ListView Name="lstviewOfOwners" ItemsSource="{Binding}" SelectionChanged="lstviewOfOwners_SelectionChanged" Grid.Row="0"> <ListView.View> <GridView> <GridViewColumn Header="First Name" Width="150" DisplayMemberBinding="{Binding FName}"/> <GridViewColumn Header="Last Name" Width="150" DisplayMemberBinding="{Binding LName}"/> <GridViewColumn Header="Date Of Purchase" Width="120" DisplayMemberBinding="{Binding DateOfPurchase}"/> <GridViewColumn Header="PlanType" Width="120" DisplayMemberBinding="{Binding PlanType}"/> </GridView> </ListView.View> </ListView> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="0.1*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Label Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Right">First Name</Label> <Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right">Last Name</Label> <Label Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Right">Date Of Purchase</Label> <Label Grid.Column="0" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Right">Plan Type</Label> <TextBox Name="txtFName" Grid.Column="2" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Width="150"></TextBox> <TextBox Name="txtLName" Grid.Column="2" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Left" Width="150"></TextBox> <TextBox Name="txtDateOfPurchase" Grid.Column="2" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Left" Width="150"></TextBox> <TextBox Name="txtPlanType" Grid.Column="2" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Left" Width="150"></TextBox> </Grid> </Grid> </Window> {/codecitation} Now we have finished GUI. lets write code to in the code file to build items and listview and when we are select the item in the list view display details in below part.Here one tricky point we need take look, in the windows form listview just work as columns and items. but in WPF it different. 1.If we are goin bind item dynamically you need set the ItemSource="{Binding}" attribute to listview. 2.Every columns would compose using GridView. First write code to bind item to listview.when the window load time write code. {codecitation class="brush: csharp; gutter: true;" width="650px"} private void lstviewOfOwners_SelectionChanged(object sender, SelectionChangedEventArgs e) { HouseOwnerInfo ownerInfo = (HouseOwnerInfo)lstviewOfOwners.SelectedItem; MakeView(ownerInfo); } {/codecitation} Within the window load event just read records from the database and bind to lisview using ItemSource property.to this we are using two support classes. First is entity class(custom object for the House Onwer Information). It's look like followings. {codecitation class="brush: csharp; gutter: true;" width="650px"}
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace CG.CS.WPFDemos.Entities { public class HouseOwnerInfo { public HouseOwnerInfo() {
} string fName;
public string FName { get { return fName; } set { fName = value; } } string lName;
public string LName { get { return lName; } set { lName = value; } } string dateOfPurchase;
public string DateOfPurchase { get { return dateOfPurchase; } set { dateOfPurchase = value; } } string planType;
public string PlanType { get { return planType; } set { planType = value; } } } }
{/codecitation} next class is support to data access with above custom object. {codecitation class="brush: csharp; gutter: true;" width="650px"}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CG.CS.WPFDemos.Entities; using System.Data.SqlClient;
namespace CG.CS.WPFDemos.DB { public class DBManager {
public static List<HouseOwnerInfo> GetCollectionOfOwners() { try { List<HouseOwnerInfo> collectionOfOwners = new List<HouseOwnerInfo>(); using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); } using (SqlCommand cmd = connection.CreateCommand()) { cmd.CommandText = @"select FName ,LName ,DateOfPurchase ,PlanType from HouseOwnerInfo;"; cmd.CommandType = System.Data.CommandType.Text; HouseOwnerInfo houseOnwer = null; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { houseOnwer = new HouseOwnerInfo(); if (reader[0] != null) { houseOnwer.FName = reader[0].ToString(); } if (reader[1] != null) { houseOnwer.LName = reader[1].ToString(); } if (reader[2] != null) { houseOnwer.DateOfPurchase = reader[2].ToString(); } if (reader[3] != null) { houseOnwer.PlanType = reader[3].ToString(); } collectionOfOwners.Add(houseOnwer); } } } }
return collectionOfOwners; } catch (Exception ex) {
throw ex; } } } }
{/codecitation} Above class read records from the database using sql reader and build a custom collection using HouseOwnerInfo object. Now everything seem good , just build project and run.output like followings figure. 
Next we need write code to access selected item in the listview and display details in below part of the window. To this we need use the SelectionChanged event in lsitview. within this we need write code like followings. {codecitation class="brush: csharp; gutter: true;" width="650px"}
private void lstviewOfOwners_SelectionChanged(object sender, SelectionChangedEventArgs e) { HouseOwnerInfo ownerInfo = (HouseOwnerInfo)lstviewOfOwners.SelectedItem; MakeView(ownerInfo); } private void MakeView(HouseOwnerInfo ownerInfo) { txtFName.Text = ownerInfo.FName; txtLName.Text = ownerInfo.LName; txtDateOfPurchase.Text = ownerInfo.DateOfPurchase; txtPlanType.Text = ownerInfo.PlanType; }
{/codecitation} Here we have important point to learn take look first line with the selectionChanged event .when we are select a item in the listview get that item and cast to HouseOwnerInfo object. why this need to do? acutlly when we are bind custom object collection to listview it's every items bind by the HouseOnwerInfo object. Since when we select a item in the listview its equal to houseOwnerInfo object. Note:we need cast to access that object to orignal object. Then we have simple method to display details to controls.it;s like normal windows form application control handling. Now build application and run, then select a item from the lsitview. it's look like followings figure. 
Complete Code {codecitation class="brush: csharp; gutter: true;" width="650px"}
//Code RRaveen (vrrave[at]codegain.com) //Offical site: http://codegain.com
//This program is free software: you can redistribute it and/or modify //it under the terms of the GNU Lesser General Public License as published by //the Free Software Foundation, either version 3 of the License, or //(at your option) any later version.
//This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Lesser General Public License for more details.
//You should have received a copy of the GNU Lesser General Public License //along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using CG.CS.WPFDemos.Entities; using CG.CS.WPFDemos.DB;
namespace CG.CS.WPFDemos { /// <summary> /// Interaction logic for wdwListViewDemo.xaml /// </summary> public partial class wdwListViewDemo : Window { public wdwListViewDemo() { InitializeComponent(); }
private void lstviewOfOwners_SelectionChanged(object sender, SelectionChangedEventArgs e) { HouseOwnerInfo ownerInfo = (HouseOwnerInfo)lstviewOfOwners.SelectedItem; MakeView(ownerInfo); } private void MakeView(HouseOwnerInfo ownerInfo) { txtFName.Text = ownerInfo.FName; txtLName.Text = ownerInfo.LName; txtDateOfPurchase.Text = ownerInfo.DateOfPurchase; txtPlanType.Text = ownerInfo.PlanType; }
private void Window_Loaded(object sender, RoutedEventArgs e) { List<HouseOwnerInfo> collectionOfOwnwer = DBManager.GetCollectionOfOwners(); lstviewOfOwners.ItemsSource = collectionOfOwnwer; } } }
{/codecitation} That's all it's cool. Conlusion This article explian about work listview in windows presentaion foundation and bind custom object lsitview. Thank you RRaveeen
|