saikamesh
saikamesh

Reputation: 4629

Metro app how to create grouped GridView in the C# code behind

In my metro app I need to show groups of VariableSizeWrapGrid on a GridView. Doing this is straight forward in XAML(by creating ItemsPanelTemplate and GroupStyle). But is there a way to do the same in C# code behind.

Upvotes: 0

Views: 1193

Answers (1)

N_A
N_A

Reputation: 19897

From here:

using System;
using System.Windows;
using System.Windows.Data;

namespace GroupingSample
{
    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        CollectionView myView;
        private void AddGrouping(object sender, RoutedEventArgs e)
        {
            myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
            if (myView.CanGroup == true)
            {
                PropertyGroupDescription groupDescription
                    = new PropertyGroupDescription("@Type");
                myView.GroupDescriptions.Add(groupDescription);
            }
            else 
                return;
        }

        private void RemoveGrouping(object sender, RoutedEventArgs e)
        {
            myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
            myView.GroupDescriptions.Clear();
        }
    }
}

The key here is that you get the default view off of the ItemsSource and set the grouping on that. This line:

myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);

Upvotes: 1

Related Questions