My webpage is an Entity Framework object. They are associated with WPF TreeView. I want to order all the web pages shown in TreeView in the Sort property.
the code
Edmx

The Subordinates property returns a collection of zero or more web pages.
Xaml
<TreeView Name="TreeViewWebpages">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Webpage}"
ItemsSource="{Binding Subordinates}">
<TextBlock Text="{Binding Path=Title}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
WITH#
TreeViewWebpages.ItemsSource = from Webpage root in db.Webpages.Include("Subordinates")
where root.Dominant == null
select root;
Result
TreeView has disordered web pages.
Problem
How to change this to arrange all the web pages displayed in the TreeView in the Sort property?
Update
This ValueConverter works (thanks @KP Adrian and @IVerzin). Is there a better way?
Xaml
ItemsSource="{Binding Path=Subordinates, Converter={local:SortConverter}}"
WITH#
[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;
}
}
source
share