Click here to Skip to main content
15,888,098 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm absolutely out of any ideas ...
I want to sort a tree by the TreeNode_SortOrder field.

Here is what I have so far:
I have created an Ado .Net entity model looking like this:
Sample Image

In fact, this is more or less a common adjacent list. I can represent a whole tree with this model. I have then created a treeview with hierarchical template, below is the xaml:
XML
<TreeView Name="TreeView1">
             <TreeView.ItemTemplate>
                 <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                                <TextBlock>
                                    <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0} - {1} -> {2}">
<Binding   Path="Question.Question_SuggestedQuestionCode"/>
                                        <Binding Path="Question.Question_Text"/>
                                        <Binding Path=".TreeNode_SortOrder"/>
                                    </MultiBinding>
                                </TextBlock.Text>
                                </TextBlock>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

This shows my tree the way I want, except one thing I cannot figure out: I want to sort the children nodes of each node by the TreeNode_SortOrder field (this is a long number field).

I did not find a way to do it in xaml nor do I get it to work programmatically. (With the help of a converter for example) I am absolutely stuck with this, any help is greatly appreciated.
Posted
Updated 31-Aug-10 7:27am
v2

1 solution

This should be no problem with a value converter.
Simple example (new WPF project, DataContext of MainWindow set to Application.Current):

MIDL
public class Node
{
    public Node(int ordinal)
    {
        this.Ordinal = ordinal;
        this.Children = new List<Node>();
    }
    public int Ordinal { get; set; }
    public List<Node> Children { get; private set; }
}


MIDL
public class NodeSortConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<Node> nodes = value as List<Node>;
        if (nodes != null)
        {
            List<Node> sorted = new List<Node>(nodes);
            sorted.Sort(new Comparison<Node>((x, y) => x.Ordinal.CompareTo(y.Ordinal)));
            return sorted;
        }
        else
            throw new ArgumentException();
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}


XML
<Window.Resources>
    <local:NodeSortConverter x:Key="nodeSorter" />
</Window.Resources>
<Grid>
    <TreeView ItemsSource="{Binding Nodes}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children, Converter={StaticResource nodeSorter}}">
                <Border Margin="3">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Ordinal}" />
                    </StackPanel>
                </Border>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid>


public partial class App : Application
{
    private Random _rand = new Random((int)DateTime.Now.Ticks);

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Nodes = new List<Node>();
        Nodes.Add(CreateNodeTree(new Node(_rand.Next()), 3, 5));
    }

    private Node CreateNodeTree(Node root, int depth, int childCount)
    {
        for (int i = 0; i < childCount; i++)
        {
            Node child = new Node(_rand.Next());
            root.Children.Add(child);
            if (depth > 0)
                CreateNodeTree(child, depth - 1, childCount);
        }
        return root;
    }

    public List<Node> Nodes { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900