Rocko Bonaparte
Topic Author
Posts: 18
Joined: 13 Oct 2020, 08:32

Alternative for CollectionViewSource.GetDefaultView

24 Dec 2020, 09:27

I was using this outside of NoesisGUI to set up a custom sort in a view model:
		protected void setUpSaveButtonSorting()
		{
			var view = (ListCollectionView)CollectionViewSource.GetDefaultView(_groupMembers);
			view.CustomSort = new CommonSaveButtonDataComparer();
		}
_groupMembers is an ObservableCollection<> of my GUI elements. I'm making sure they are ordered a certain way based on the view model data represented by the elements.

I thought this was kind of a goofy thing to do, but I do need to code the sorting logic; it's not a simple field based sort. NoesisGUI seems to know what a CollectionViewSource is but doesn't result GetDefaultView(). I also couldn't resolve ListCollectionView at all.

Should I be doing something else to be compatible with NoesisGUI?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: Alternative for CollectionViewSource.GetDefaultView

24 Dec 2020, 11:53

For sorting we recommed using our CollectionSortBehavior, which allows you to specify the source collection and a comparer object used for sorting the collection in any way you want. We provide a StringSortComparer for simple cases, or you can use it as reference to create your own.
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
  <i:Interaction.Behaviors>
    <noesis:CollectionSortBehavior x:Name="SortBehavior"
      ItemsSource="{Binding Books}"
      Comparer="{Binding BookComparer}" />
  </i:Interaction.Behaviors>
  <ListBox ItemsSource="{Binding SortedItems, ElementName=SortBehavior}" />
</Grid>
 
Rocko Bonaparte
Topic Author
Posts: 18
Joined: 13 Oct 2020, 08:32

Re: Alternative for CollectionViewSource.GetDefaultView

24 Dec 2020, 18:17

Does the SortComparer require more context than a regular .NET Comparer? It looks like I need to be able to tell it when the collection has changed so that it can sort again. It gave me some anxiety since I don't have that kind of plumbing yet.

Do you have an ideas on how I can define this custom ItemSource in the XAML while being able to still run it in a normal WPF context? I don't know how I can bring that noesis namespace in and conditional set my item source based on it. Or, does this happen to be something that can work fine outside of Noesis' runtime?
 
User avatar
sfernandez
Site Admin
Posts: 2056
Joined: 22 Dec 2011, 19:20

Re: Alternative for CollectionViewSource.GetDefaultView

28 Dec 2020, 10:38

Hi, CollectionSortBehavior and SortComparer classes are part of our Noesis.GUI.Extensions nuget package for Blend, so you can just include it in your Blend project to share the same code and xaml.

SortComparer is similar to a regular .NET comparer (Compare method implementation), but it allows you to minimize the situations when sorting is required by indicating when changes in the collection items are relevant for sorting (NeedsRefresh method implementation). If you return false in the NeedsRefresh method it would mean that item property changes won't affect sorting, otherwise returning true would mean that property changes will sort the collection. Or you can use the provided item and name of the changed property to determine if a sort is required.

Who is online

Users browsing this forum: No registered users and 0 guests