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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » ObservableCollectionScreenshots 
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>)>).
  


<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"));
        }
    }
}

   
    
  
Related examples in the same category
1.Use ObservableCollection as ResourceUse ObservableCollection as Resource
2.Bind to ObservableCollectionBind to ObservableCollection
3.Bind to ObservableCollection and ItemsSourceBind to ObservableCollection and ItemsSource
4.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.