gpt4 book ai didi

c# - uwp 中的 ListView 组

转载 作者:太空狗 更新时间:2023-10-29 22:21:38 24 4
gpt4 key购买 nike

我有一个带有 ListView 的简单 View ,我希望 ListView 有两个组,一组用于属性为 complete= 1 和另一组 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 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 来呈现可分组或排序的项目列表的内容。

<Page.Resources>
<CollectionViewSource x:Key="cvs" x:Name="cvs" IsSourceGrouped="True" />
</Page.Resources>

然后,获取数据,对数据进行分组,在后台代码中将分组后的数据设置到CollectionViewSource

以下是我验证过的示例代码:

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;
}
}

输出如下: enter image description here

关于c# - uwp 中的 ListView 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37122502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com