none
How to Pass Sort Direction to Converter

    Question

  • I have successfully built a sorter converter into my TreeView.  The XAML snippet is:

    <TreeView.ItemTemplate>
    	<HierarchicalDataTemplate
    		ItemsSource="{Binding Path=ChildNodes, Converter={StaticResource Sorter}, ConverterParameter=Name}">
    		<TextBlock Text="{Binding Path=Name}" />
    	</HierarchicalDataTemplate>
    </TreeView.ItemTemplate>

    and the Sorter snipper is:

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
    	Dim collection As IList = TryCast(value, IList)
    	Dim view As New ListCollectionView(collection)
    	Dim sort As New SortDescription(parameter.ToString(), ListSortDirection.Ascending)
    	view.SortDescriptions.Add(sort)
    	Return view
    End Function
    The only problem is that the sort direction is hard wired to be ascending.  How do I make the sort direction a parameter and how do I specify that parameter in the XAML?

    Monday, March 13, 2017 5:23 AM

Answers

  • The answer ultimately was to user a Multi-Converter and pass the sort direction in as a second, separate "value".
    • Marked as answer by SezMe Thursday, March 16, 2017 4:00 AM
    Thursday, March 16, 2017 3:54 AM

All replies

  • Hello,

    Is this a WPF project?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, March 13, 2017 9:03 AM
    Moderator
  • You would use the ConverterParameter in the XAML.

    An example from one of my apps:

    <Grid Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=UP, ElementName=ShowHideContents}" >

    The ConverterParameter can be a string like Up/Down to indicate the sort order.


    Lloyd Sheen

    Monday, March 13, 2017 10:27 AM
  • Yes, but the issue is how to write the VB sorter so it belongs in the VB subforum.
    Thursday, March 16, 2017 3:53 AM
  • The answer ultimately was to user a Multi-Converter and pass the sort direction in as a second, separate "value".
    • Marked as answer by SezMe Thursday, March 16, 2017 4:00 AM
    Thursday, March 16, 2017 3:54 AM
  • I use the ConverterParameter to tell the converter which value to sort on.  If I use it to define the sort direction, how would it know which value to sort on?
    Thursday, March 16, 2017 3:57 AM