c# - uwp 中的 ListView 组

标签 c# xaml listview win-universal-app

我有一个带有 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/

相关文章:

android - 如何制作水平和垂直滚动的二维图片库?

c# - 正则表达式与字符串不匹配

c# - 如何在ControlTemplate中绑定(bind)ComboBox的ItemsSource?

c# - IValueConverter 的最佳实践是什么?

c# - 如何在 DataGridTextColumn 上使用 MultiBinding?

Java/Android Arraylist 给出地址

android - 按下后退按钮时如何刷新 fragment ?

c# - 当系统处于休眠状态时,Windows 服务中的计时器如何运行?

c# - C#中的Excel互操作中的HRESULT代码

c# - 返回 IQueryable 时覆盖 DbContext