Reputation: 4629
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
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