Listview group in uwp

狂风中的少年 提交于 2019-12-05 00:45:39

问题


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.

This is my class:

public class myClass
{
    public string name{ get; set; }
    public bool complete{ get; set; }
}

This is my 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.


回答1:


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.

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>

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

Following is the output:



来源:https://stackoverflow.com/questions/37122502/listview-group-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!