[英]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. 在我的Metro应用程序中,我需要在GridView上显示多组VariableSizeWrapGrid。 Doing this is straight forward in XAML(by creating ItemsPanelTemplate and GroupStyle). 在XAML中这样做很简单(通过创建ItemsPanelTemplate和GroupStyle)。 But is there a way to do the same in C# code behind. 但是后面有没有办法在C#代码中做同样的事情。
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. 此处的关键是您可以从ItemsSource
获取默认视图并在其上设置分组。 This line: 这行:
myView = (CollectionView)CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.