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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » ObservableCollection 
24.148.3.Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).
<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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.