树视图的分层数据模板类

时间:2012-03-14 18:23:25

标签: c# wpf treeview

我想在我的项目中为我的树视图实现分层数据模板。 结构看起来应该是这样的:

- ManagedItems (RootItem)
     - Department 1
         -Client 1
             - Feature 1
             - Feature 2
         -Client 2
             - Feature 1
     - Department 2
             - Department 4
                     - Department 5
                            -Client 4
                                   - Feature 1
     - Department 3
         -Client 3
             - Feature 1
             - Feature 2
             - Feature 3


//Current class for datatype
public class Entries
{
    public string Department { get; set; }
    public string Client { get; set; }

    // ?? How to implement a drive array and a feature array which will be displayed correctly in the treeview?

    public ObservableCollection<Entries> Children { get; set; }
}

它在C#和WPF(.NET 4.0)中。 一些想法?

感谢。

1 个答案:

答案 0 :(得分:1)

适用于您的型号的线框:

class ManagedItems {
   IList<Department> Departments // TreeView.ItemsSource
}
class Department {// HierDT
   IList<object> Children // HierDT.ItemsSource; (can either be Department or Client)
}
class Client {// HierDT
   IList<Feature> Features // HierDT.ItemsSource
}
class Feature { } // normal DataTemplate

Department中定义ClientHierDTs Feature以及DataTemplate TreeView.Resources

注意Department.Childrenobjects。这没关系,因为当WPF“看到”Client时,它会“膨胀”到相应的HierDT(以及当它“看到”Department时,它会膨胀到Department已定义HierDT)。