How would you go about using a CollectionViewSource (to supply sorting behavior) in conjunction with a HierarchicalDataTemplate's ItemsSource?
So given the code below, how could I apply sorting on the children at each level in the hierarchy?
<HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}"
ItemsSource="{Binding Children}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command"
Value="{Binding Command}" />
<Setter Property="CommandParameter"
Value="{Binding}" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
I thought I might have been able to do the following but it breaks descendants from showing as well as producing the following binding error.
<HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}">
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Children}" />
</Binding.Source>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command"
Value="{Binding Command}" />
<Setter Property="CommandParameter"
Value="{Binding}" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Children; DataItem=null; target element is 'CollectionViewSource' (HashCode=5114324); target property is 'Source' (type 'Object')
Edit : I ended up with the following - hope it helps someone else
<Window.Resources>
<l:SortedCollectionViewSource x:Key="SortedCollectionViewSource" Property="Name"/>
</Window.Resources>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="Add new item..."
ItemsSource="{Binding AddNewItem.Children, Converter={StaticResource SortedCollectionViewSource}}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type l:CommandGroup}"
ItemsSource="{Binding Children, Converter={StaticResource SortedCollectionViewSource}}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Command"
Value="{Binding Command}" />
<Setter Property="CommandParameter"
Value="{Binding}" />
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
</Window.ContextMenu>
public class SortedCollectionViewSource : IValueConverter
{
public string Property { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var cvs = new CollectionViewSource() { Source = value };
cvs.SortDescriptions.Add(new SortDescription(Property, ListSortDirection.Ascending));
return cvs.View;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}