Help with displaying listview group header

Mar 7 2009 11:09 PM

I'm having trouble displaying the group header of a listview grouping I made. Can you please check it out. The whole code is below. Just a simple load of items and applying the grouping but for some reason, the binding on the datatemplate won't display. Please help and thank you very much!



Code behind:

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.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private List<Student> _StudentList = new List<Student>();
        public Window1()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {                       
            _StudentList.Add(new Student() { FirstName = "Shaquille", LastName = "O'Neal", Level = "Grade 1" });
            _StudentList.Add(new Student() { FirstName = "Michael", LastName = "Jordan", Level = "Grade 1" });
            _StudentList.Add(new Student() { FirstName = "Mark", LastName = "Price", Level = "Grade 2" });
            _StudentList.Add(new Student() { FirstName = "Allen", LastName = "Iverson", Level = "Grade 2" });
            _StudentList.Add(new Student() { FirstName = "LeBron", LastName = "James", Level = "Grade 2" });
            _StudentList.Add(new Student() { FirstName = "Kobe", LastName = "Bryant", Level = "Grade 3" });
            _StudentList.Add(new Student() { FirstName = "Chanucey", LastName = "Billups", Level = "Grade 1" });

            InitLvw();
        }

        private void InitLvw()
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(_StudentList);
            if (view.CanGroup)
                view.GroupDescriptions.Add(new PropertyGroupDescription("Level"));
           
            // Get template.
            GroupStyle gs = new GroupStyle();
            gs.HeaderTemplate = this.FindResource("categoryTemplate") as DataTemplate;
            lvw.GroupStyle.Add(gs);
            lvw.SelectionMode = SelectionMode.Single;

            // View.
            GridView gv = new GridView();           
            GridViewColumn gvc = new GridViewColumn();
            gvc = new GridViewColumn();
            gvc.Header = "First Name";
            gvc.Width = 90;
            gvc.DisplayMemberBinding = new Binding("FirstName");
            gv.Columns.Add(gvc);

            gvc = new GridViewColumn();
            gvc.Header = "Last Name";
            gvc.Width = 90;
            gvc.DisplayMemberBinding = new Binding("LastName");
            gv.Columns.Add(gvc);
            gvc = new GridViewColumn();

            lvw.View = gv;
            lvw.ItemsSource = _StudentList;
        }

        public class Student
        {
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
            public string Level { get; set; }
        }
    }
}




And the XAML:

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
   
    <Window.Resources>
        <DataTemplate x:Key="categoryTemplate">
            <TextBlock Text="{Binding Path=Level}" FontWeight="Bold" Foreground="ForestGreen" Margin="0,5,0,0"/>
        </DataTemplate>
    </Window.Resources>
   
    <Grid>
        <ListView x:Name="lvw" />
    </Grid>
</Window>


Answers (1)