【问题标题】:ListView Grouping without using IGrouping<TKey, TModel>不使用 IGrouping<TKey, TModel> 的 ListView 分组
【发布时间】:2021-06-07 03:42:39
【问题描述】:

有没有办法让我将我的数据分组到 ListView 中而无需实现我使用的集合

IGrouping<TKey, TModel>

我正在使用这个VirtualCollection。然后,当我在 ListViewModel 上实现 IGrouping 并将 CollectionViewSource 添加到我的页面时,VirtualCollection 在向下滚动时永远不会添加新项目。分组确实有效,我可以看到我的 ListView 标题,但永远不会添加更多数据。

所以我的理想世界是,如果我不必实现 IGrouping 接口并且仍然获得对我的 ListView 数据进行分组的所需结果,而且我不会有 2 个数据列表,1 个用于所有项目,1 个用于对于持有项目的组。

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    您可以通过不实现IGrouping&lt;TKey, TModel&gt; 在列表视图中对数据进行分组。请参考以下示例。

    XAML 代码:

    <Page
       ..>
       <Page.Resources>
            <CollectionViewSource x:Name="cvs" x:Key="cvs" IsSourceGrouped="True"/>
       </Page.Resources>
        <Grid>
            <ListView ItemsSource="{x:Bind cvs.View,Mode=OneWay}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="local:Person">
                        <TextBlock Text="{x:Bind name}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.GroupStyle >
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate >
                            <DataTemplate>
                                <TextBlock Text="{Binding Key}"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>
        </Grid>
    </Page>
    

    后面的代码:

    using System.Collections.ObjectModel;
    using System.Linq;
    using Windows.UI.Xaml.Controls;
    
    namespace GroupingListView
    {
       public sealed partial class MainPage : Page
        {
            public ObservableCollection<Person> persons { get; set; }      
            public MainPage()
            {
                this.InitializeComponent();
                persons = new ObservableCollection<Person>()
                {
                    new Person(){name="Tom",job="teacher",age=27},
                    new Person(){name="Jack",job="lawyer",age=29},
                    new Person(){name="Lily",job="teacher",age=22},
                    new Person(){name="Arice",job="doctor",age=23},
                    new Person(){name="Ethan",job="teacher",age=22},
                    new Person(){name="Kiki",job="lawyer",age=27},
                    new Person(){name="Sherry",job="doctor",age=20},
                    new Person(){name="Json",job="lawyer",age=26},
                    new Person(){name="Jovi",job="doctor",age=23},
                    new Person(){name="Seria",job="teacher",age=25},
                };
                var group = from p in persons group p by p.job;
                this.cvs.Source = group;
            }
        }
        public class Person
        {
            public string name { get; set; }
            public string job { get; set; }
            public int age { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 2010-12-07
      相关资源
      最近更新 更多