0

我有这个问题,我无法将我的数据加载到我的 TreeView 中,但我必须手动进行。我的静态解决方案如下所示:

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

这可行,但正如您所见,它只有三个级别,我想要动态级别的数量。如果有人想知道我的 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]);
4

1 回答 1

0

尝试结合HierarchicalDataTemplate控件实现INotifyPropertyChanged 接口,这应确保对底层数据结构所做的更改反映在 TreeView 结构中。

于 2013-02-11T09:00:28.417 回答