Page 1 of 1

Alternative for CollectionViewSource.GetDefaultView

Posted: 24 Dec 2020, 09:27
by Rocko Bonaparte
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?

Re: Alternative for CollectionViewSource.GetDefaultView

Posted: 24 Dec 2020, 11:53
by sfernandez
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>

Re: Alternative for CollectionViewSource.GetDefaultView

Posted: 24 Dec 2020, 18:17
by Rocko Bonaparte
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?

Re: Alternative for CollectionViewSource.GetDefaultView

Posted: 28 Dec 2020, 10:38
by sfernandez
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.