如何重新排序UltraTree中的节点?

时间:2011-11-09 15:46:27

标签: .net infragistics ultratree

我有一个看起来像这样的课程:

public class GeneralStatusInfo
{        
    public List<string> List_BLNumber { get; set; }
    public List<POInfo> List_PONumbers { get; set; }
    public List<string> List_Pickup { get; set; }
    public List<string> List_Origin { get; set; }
    public List<string> List_Destination { get; set; }
    public List<string> List_NotifyName { get; set; }
    public List<AppmntInformation> List_Appointments { get; set; }
}

当我绑定这样的数据时:

List<GeneralStatusInfo> statusBind = new List<GeneralStatusInfo>();
statusBind.Add(status);
utGeneralStatusInfo.DataSource = statusBind;

SetupTree(status);

它将我的父节点放在不同的顺序:

 Appointment 
 P/O Number 
 B/L Number 
 Origin 
 Pickup 
 Notify 
 Payment
 Destination

如何对节点进行重新排序,使它们的排列方式与我在课堂上的顺序相同?

1 个答案:

答案 0 :(得分:1)

您必须制作自己的 IComparer 。像这样:

public class  GeneralStatusInfoMemberOrderComparer: IComparer
{
    public  GeneralStatusInfoMemberOrderComparer()
    {
        memberOrdermemberOrder.Add("B/L Number",0);
        memberOrdermemberOrder.Add("P/O Number",1);
        /// 
        /// add more items
        ///
    }

    private bool sortAlphabetically=false;
    private Dictionary<string,int> memberOrder  = new Dictionary<string,int>();

    public bool SortAlphabetically
    {
        get{return sortAlphabetically;}
        set{sortAlphabetically = value;}
    }

    int IComparer.Compare(object x, object y)
    {
        string propertyX = x as string;
        string propertyY = y as string;

        if (sortAlphabetically)
        {
            return propertyX.CompareTo(propertyY);
        }
        else
        {
            int orderX= memberOrder.ContainsKey(propertyX) ? memberOrder[propertyX] : -1;
            int orderY= memberOrder.ContainsKey(propertyY) ? memberOrder[propertyY] : -1;
            return orderX.CompareTo(orderY);
        }
    }
}

然后只需设置UltraTree实例的 SortComparer 属性:

ultraTree1.SortComparer = new GeneralStatusInfoMemberOrderComparer();