))." src="http://www.rntsoft.com/Code/CSharpImages/WPF-CreateAListViewControlThatUsesAGridViewViewModeToDisplayTheContentsOfAnObservableCollectionOfT.PNG"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="OnLoad" xmlns:ds="clr-namespace:WpfApplication1">
ListView created with XAML
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
public partial class Window1 : Window
{
void OnLoad(object sender, RoutedEventArgs e)
{
ListView myListView = new ListView();
GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true;
myGridView.ColumnHeaderToolTip = "Employee Information";
GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("FirstName");
gvc1.Header = "FirstName";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("EmployeeNumber");
gvc3.Header = "Employee No.";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);
myListView.ItemsSource = new myEmployees();
myListView.View = myGridView;
myStackPanel.Children.Add(myListView);
}
}
public class EmployeeInfo
{
private string _firstName;
private string _employeeNumber;
public string FirstName
{
get {return _firstName;}
set {_firstName = value;}
}
public string EmployeeNumber
{
get {return _employeeNumber;}
set {_employeeNumber = value;}
}
public EmployeeInfo(string firstname, string empnumber)
{
_firstName = firstname;
_employeeNumber = empnumber;
}
}
public class myEmployees : ObservableCollection
{
public myEmployees()
{
Add(new EmployeeInfo("A", "1"));
Add(new EmployeeInfo("B", "9"));
Add(new EmployeeInfo("C", "2"));
Add(new EmployeeInfo("D", "4"));
}
}
}