My Webpage is an Entity Framework entity. These are bound to a WPF TreeView. I want to order all the Webpages shown in the TreeView on the Sort property.
Code
EDMX
Its Subordinates property returns a collection of zero or more Webpages.
XAML
<TreeView Name="TreeViewWebpages">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Webpage}"
ItemsSource="{Binding Subordinates}">
<TextBlock Text="{Binding Path=Title}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
C#
TreeViewWebpages.ItemsSource = from Webpage root in db.Webpages.Include("Subordinates")
where root.Dominant == null
select root;
Result
Webpages are unordered within the TreeView.
Problem
How do I change this to order all the Webpages shown in the TreeView on the Sort property?
Update
This ValueConverter seems to work (Thank you @KP Adrian and @IVerzin). Is there a better way?
XAML
ItemsSource="{Binding Path=Subordinates, Converter={local:SortConverter}}"
C#
[ValueConversion(typeof(EntityCollection<Webpage>), typeof(EntityCollection<Webpage>))]
public class SortConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((EntityCollection<Webpage>)value).OrderBy(o => o.Sort);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}