2013-02-11 21 views
0

我有这个问题,我不能将我的数据加载到我的TreeView中,但我必须手动执行它。我的静态的解决方案是这样的:TreeView中的动态ObservableCollection?

public static ObservableCollection<ClassOfDoom> GetAll() 
    { 
     ObservableCollection<ClassOfDoom> listToReturn = new ObservableCollection<ClassOfDoom>(); 

     ClassOfDoom treeItem = null; 

     OUViewModel ouvm = new OUViewModel(); 
     int[] tempOU = ouvm.HierarchyIntOU; 
     int[] tempP = ouvm.HierarchyIntParent; 

     treeItem = new ClassOfDoom("Root"); 
     treeItem.cID = tempOU[0]; 
     treeItem.pID = tempP[0]; 
     for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++) 
     { 
      if (treeItem.cID == tempP[i]) 
      { 
       treeItem.ClassOfDooms.Add(new ClassOfDoom(tempOU[i].ToString())); 
       treeItem.ClassOfDooms.Last().pID = tempP[i]; 
       treeItem.ClassOfDooms.Last().cID = tempOU[i]; 
       for (int i1 = 0; i1 < ouvm.HierarchyIntParent.Length; i1++) 
       { 
        if (treeItem.ClassOfDooms.Last().cID == tempP[i1]) 
        { 
         treeItem.ClassOfDooms.Last().Countries.Add(new ClassOfDoom(tempOU[i1].ToString())); 
        } 
       } 
      } 
     } 
     listToReturn.Add(treeItem);  
     return listToReturn; 
    } 

这工作,但你可以看到it's只有三个级别,我wan't水平的动态量。如果有人怀疑我的ClassOfDooms名单如下:

public ObservableCollection<ClassOfDoom> ClassOfDooms 
{ 
    get 
    { 
     if (classOfDooms == null) classOfDooms = new ObservableCollection<ClassOfDoom>(); 
     return classOfDooms; 
    } 
    set { classOfDooms = value; } 
} 

我想再次声明,我能够读懂从我的数据库之类的东西的数据。 TreeView只是获取正确的信息并非全部。

编辑:我解决我自己是这样的:

ClassOfDoom[] asdfDooms = new ClassOfDoom[ouvm.HierarchyIntParent.Length]; 
    for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++) 
    { 
     asdfDooms[i] = new ClassOfDoom(); 
     asdfDooms[i].cID = tempOU[i]; 
     asdfDooms[i].pID = tempP[i]; 
     asdfDooms[i].name = tempPersonName + asdfDooms[i].cID.ToString(); 
    } 
    for (int aint = 0; aint < ouvm.HierarchyIntParent.Length; aint++) 
    { 
     for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++) 
     { 
      if (asdfDooms[aint].cID == asdfDooms[i].pID) 
      { 
       asdfDooms[aint].classOfDooms.Add(asdfDooms[i]); 
      } 
     } 
    } 
    listToReturn.Add(asdfDooms[0]); 

回答

0

尝试实现与HierarchicalDataTemplate控制相结合的INotifyPropertyChanged interface,这应该能保证底层数据结构所做的更改会反映在TreeView结构。

+0

我看着INotifyPropertyChanged接口很快,但没有完全理解我如何在我的代码中实现这一点。你可以给我一个例子吗?你需要更多信息吗? – 2013-02-11 09:03:51

+0

看看http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx,它给出了一些背景知道为什么它是必要的以及在哪里使用它。 – 2013-02-11 09:06:22

相关问题