按自定义字段对TreeView进行排序 [英] Sorting a TreeView by a custom field

查看:182
本文介绍了按自定义字段对TreeView进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对没有任何想法...
我想按TreeNode_SortOrder字段对树进行排序.

这是我到目前为止的内容:
我创建了一个Ado .Net实体模型,如下所示:
样本图像

实际上,这或多或少是一个常见的相邻列表.我可以用这个模型代表一棵完整的树.然后,我用分层模板创建了一个树形视图,下面是xaml:

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:

<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>


这以我想要的方式显示了我的树,除了我无法弄清楚的一件事:我想通过TreeNode_SortOrder字段(这是一个长数字字段)对每个节点的子节点进行排序.

我没有找到在xaml中执行此操作的方法,也没有使其以编程方式工作. (例如,在转换器的帮助下),我对此一无所知,非常感谢.


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.

推荐答案

这对于值转换器应该没有问题.
简单示例(新的WPF项目,MainWindow的DataContext设置为Application.Current):

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

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; }
}





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
}





<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; }
}


这篇关于按自定义字段对TreeView进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆