UWP中的Listview组 [英] Listview group in uwp
问题描述
我有一个带有ListView
的简单视图,我希望ListView
有两个组,一个具有属性complete= 1
的项目组,另一个具有complete= 0
的组.
I have a simple view with a ListView
, I would like the ListView
has two groups, a group for items with property complete= 1
and another group with complete= 0
.
这是我的课程:
public class myClass
{
public string name{ get; set; }
public bool complete{ get; set; }
}
这是我的XML:
<ListView x:Name="MasterListView">
<ListView.GroupStyle>
<GroupStyle >
<GroupStyle.HeaderTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<DataTemplate x:Key="MasterListViewItemTemplate" x:DataType="model:myClass">
<TextBlock Margin="0,5,5,5" Text="{x:Bind name}" FontSize="20" Style="{ThemeResource BaseTextBlockStyle}" />
</DataTemplate>
我尝试了一些示例,但找不到任何东西.
I tried a few examples but I could not find anything.
推荐答案
我有一个带ListView的简单视图,我希望ListView有两个组,一个用于属性为complete = 1的项目组,另一个用于complete = 0的组.
I have a simple view with a ListView, I would like the ListViewhas two groups, a group for items with property complete= 1 and another group with complete= 0.
首先,将 CollectionViewSource 用于显示可以分组或排序的项目列表的内容.
First, use a CollectionViewSource for content that presents a list of items that can be grouped or sorted.
<Page.Resources>
<CollectionViewSource x:Key="cvs" x:Name="cvs" IsSourceGrouped="True" />
</Page.Resources>
然后,获取数据,对数据进行分组,并在后面的代码中将分组的数据设置为CollectionViewSource .
Then, get the data, group the data and set the grouped data to the CollectionViewSource in code behind.
以下是我已验证的示例代码:
Following is the sample code I have verified:
MainPage.xaml
<Page.Resources>
<!--Use a collection view source for content that presents a list of items that can be grouped or sorted.-->
<CollectionViewSource x:Key="cvs" x:Name="cvs" IsSourceGrouped="True" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Background="White" Foreground="Black" SelectionMode="None" ItemsSource="{Binding Source={StaticResource cvs}}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0.5">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
MainPage.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
//Get the data
List<MyClass> myClasses = new List<MyClass>();
myClasses.Add(new MyClass { Name = "A", Complete = false });
myClasses.Add(new MyClass { Name = "B", Complete = true });
myClasses.Add(new MyClass { Name = "C", Complete = true });
myClasses.Add(new MyClass { Name = "D", Complete = false });
myClasses.Add(new MyClass { Name = "E", Complete = true });
myClasses.Add(new MyClass { Name = "F", Complete = false });
//Group the data
var groups = from c in myClasses
group c by c.Complete;
//Set the grouped data to CollectionViewSource
this.cvs.Source = groups;
}
}
以下是输出:
这篇关于UWP中的Listview组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!