2011-11-09 46 views
1

我有一個類,它看起來像這樣:如何重新排列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

您必須自己製作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();