2

In a UWP app, I can set a CollectionViewSource's Source to a List and it will group it right off the bat. With WPF it seems to work differently. I want to be able to group my list in code behind and just feed it the collection instead of listview doing it for me with the PropertyGroupDescription.

public class MyGroup : ObservableCollection<MyClass>
{
    public int ID{ get; set; }

    public MyGroup (IEnumerable<MyClass> items) : base(items)
    {
        ID= items.First().ID;
    }
}

//other code

collectionViewSource.Source = myGroup;

Then I just bind the collectionViewSource to the ItemSource. How can I do something like this in WPF?

Noam M
  • 3,156
  • 5
  • 26
  • 41
shady
  • 1,076
  • 2
  • 13
  • 33
  • Your mean of WPF mean XAML? – Firoz Apr 15 '16 at 17:54
  • Well they both use XAML. I'm trying to port a Windows app to a WPF app. the grouping in listviews work a little different in WPF. – shady Apr 15 '16 at 18:40
  • You want to sort the listitems of the observable collection and then after you have sorted them you want to bind the listview (xaml) to your collection? Should all items in your collection be visible for the user? @shady – Matthias Herrmann Apr 16 '16 at 19:26
  • yes. say a list contains objects with id 13, 14, 13, 15. using GroupBy, There will be 3 groups. if you've ever seen UWP groups you just set the source to the list of groups produced by GroupBy and set it to the sourc3 of the collectionviewsource. then set that to the listview itemsource. it automatically groups them up in the list by that. – shady Apr 17 '16 at 16:41
  • Can you post some of your XAML? Also, in `collectionViewSource.Source = myGroup;`, what is `myGroup`? – vesan Apr 18 '16 at 03:22
  • you could take a look at this answer http://stackoverflow.com/a/19946785/5233410 – Nkosi Apr 18 '16 at 03:40
  • Does my answer helps? – Noam M Apr 18 '16 at 07:31
  • -1! - The code you've attached gives 0 useful information. Please add an actual snippet of code (even if abstracted by name) that you want grouping to work with. Please provide both Xaml & C# parts of it. Given the very little information I have here, you need to set CollectionView.GroupDescription in Xaml or C#. MSDN away! – Maverik Apr 18 '16 at 11:54

1 Answers1

0

So the difference between UWP and WPF seems from my experience is that in UWP you pre group with ByGroup then feed a List to the CollectionViewSource. In WPF you feed the CollectionViewSource with List then it groups it itself. In UWP The datacontext for the HeaderTemplate is MyGroup. In WPF the datacontext is CollectionViewGroup. I just passed a different object through the binding and it worked.

shady
  • 1,076
  • 2
  • 13
  • 33
  • great - and we as community got nothing out of the effort put into this question's comments and the (deleted) answer. Sigh. why bother with such a question and answer? you could have saved time by simply deleting the information-less question in first place. – Maverik Apr 21 '16 at 12:55