Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>). : ObservableCollection « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="WpfApplication1.Window1"
    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">
 
  <Window.Resources>
    <ObjectDataProvider x:Key="EmployeeInfoDataSource" ObjectType="{x:Type ds:myEmployees}"/>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="50"/>
      <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition/>
      <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" FontSize="14" HorizontalAlignment="Center">
      ListView created with XAML
    </TextBlock>
    <StackPanel Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center">
      <ListView ItemsSource="{Binding Source={StaticResource EmployeeInfoDataSource}}">
        <ListView.View>
          <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Employee Information">
            <GridViewColumn DisplayMemberBinding= "{Binding Path=FirstName}" Header="First Name" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=EmployeeNumber}" Header="Employee No." Width="100"/>
          </GridView>
        </ListView.View>
      </ListView>
    </StackPanel>
    <StackPanel Grid.Row="1" Grid.Column="1"  Name="myStackPanel" HorizontalAlignment="Center">
    </StackPanel>
  </Grid>
</Window>
//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<EmployeeInfo>
    {
        public myEmployees()
        {
            Add(new EmployeeInfo("A",  "1"));
            Add(new EmployeeInfo("B",  "9"));
            Add(new EmployeeInfo("C",  "2"));
            Add(new EmployeeInfo("D",  "4"));
        }
    }
}
WPF Create A List View Control That Uses A Grid View View Mode To Display The Contents Of An Observable Collection Of T








24.148.ObservableCollection
24.148.1.Use ObservableCollection as ResourceUse ObservableCollection as Resource
24.148.2.Bind to ObservableCollectionBind to ObservableCollection
24.148.3.Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).
24.148.4.Bind to ObservableCollection and ItemsSourceBind to ObservableCollection and ItemsSource
24.148.5.Apply Custom Sorting Logic to a CollectionApply Custom Sorting Logic to a Collection