Quantcast
Channel: Grouping of items in ListView WPF
Viewing all articles
Browse latest Browse all 5

Grouping of items in ListView WPF

$
0
0

>>I am not able to use CollectionViewSource since my VM contains the collection of ClassA object (which is bound as item source to the listview) and I want to group the items based on one property of those ClassA objects.

You should either group the source collection in the view model or use a data source where each item contains a collection of child items. Please refer to MSDN for more information and examples of both approaches:

How to group items in a list or grid (XAML): https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780627.aspx

In your case, you could add a property to the MyVM class that returns a grouped collection:

public class ClassA
    {
        public DateTime DateTimePropertyOfClassA { get; set; }
    }

    public class MyVM
    {
        public MyVM()
        {
            //return a grouped collection:
            Grouped = from x in CollectionOfClassA group x by x.DateTimePropertyOfClassA into grp orderby grp.Key select grp;
        }

        public IList<ClassA> CollectionOfClassA { get; set; } = new List<ClassA>()
        {
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-01-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA = DateTime.Parse("2016-03-01")},
            new ClassA(){ DateTimePropertyOfClassA =DateTime.Parse("2016-06-01")}
        };

        public IEnumerable<object> Grouped { get; }
    }

<Page.Resources><CollectionViewSource x:Name="cvs"
                              IsSourceGrouped="True"
                              Source="{x:Bind MyVM.Grouped, Mode=OneWay}"/></Page.Resources><StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><ListView ItemsSource="{Binding Source={StaticResource cvs}}"><ListView.GroupStyle><GroupStyle><GroupStyle.HeaderTemplate><DataTemplate><TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Key}"/></DataTemplate></GroupStyle.HeaderTemplate></GroupStyle></ListView.GroupStyle></ListView></StackPanel>


If you don't want to modify the view model class for some reason, you could perform the grouping in the view:

<CollectionViewSource x:Name="cvs" IsSourceGrouped="True"/>

public sealed partial class BlankPage1 : Page
    {
        public MyVM MyVM { get; set; } = new MyVM();
        public BlankPage1()
        {
            this.InitializeComponent();
            var cvs = Resources["cvs"] as CollectionViewSource;
            cvs.Source = from x in MyVM.CollectionOfClassA group x by x.DateTimePropertyOfClassA into grp orderby grp.Key select grp;
        }
    }

But you should still use a CollectionViewSource.

>>Also, if there is no item for any particular day, I would still like to show that date with empty list under that group (for that day). How can I achieve it?

Only by adding an empty ClassA object to the CollectionOfClassA source collection for each "blank" date. There is no way to display property values from object that don't exist.

Hope that helps.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


Viewing all articles
Browse latest Browse all 5

Trending Articles