4

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

Entity Framework EDMX entity

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;
    }
}
Zack Peterson
  • 56,055
  • 78
  • 209
  • 280
  • 1
    If you want to sort both top-level and subordinate-level pages, use a value converter: http://stackoverflow.com/questions/5722835/how-to-sort-treeview-items-using-sortdescriptions-in-xaml – Adrian May 27 '11 at 18:59
  • 1
    Yes, EntityCollection does not implement IList, but you could cast value to an instance of your EntityCollection and call ToList() on it. -- System.Collections.IList collection = (value as EntityCollection).ToList(); – Adrian May 27 '11 at 19:22
  • Note that this will only work effectively if you have a single HierarchicalDataTemplate. If you have multiple HierarchicalDataTemplate selected by the DataType atrribute, using this method will sort the list multiple times which may slow things down. I haven't yet found a solution to bypass this issue. – Etienne Oct 27 '11 at 19:51

1 Answers1

0

Assuming your Sort property is a string or integer you are using to determine the order at runtime, you can add an orderby section to your expression.

TreeViewWebpages.ItemsSource = from Webpage root in db.Webpages.Include("Subordinates")
                           where root.Dominant == null
                           orderby root.Sort
                           select root;
Adrian
  • 1,338
  • 8
  • 17