开发者

How to use {Binding Path=/} for displaying directly third level of nested collections?

开发者 https://www.devze.com 2023-02-10 00:39 出处:网络
I have a very nested entity structure like this: DataObject > Universities (List) > Colleges (List) > Students (List) > Projects (List)

I have a very nested entity structure like this:

DataObject > Universities (List) > Colleges (List) > Students (List) > Projects (List)

I want to bind a TreeView with directly my DataObject as:

myTreeView.ItemSource = DataObject.Universities;

        DataTemplate dataTemplate = new DataTemplate();
        dataTemplate.DataType = typeof(Student);
        FrameworkElementFactory 开发者_C百科spFactoryDeliverable = new FrameworkElementFactory(typeof(TextBlock));
        spFactoryDeliverable.Name = "spFactoryDeliverable";
        spFactoryDeliverable.SetBinding(TextBlock.TextProperty, new Binding("Colleges/Students/Name"));
        dataTemplate.VisualTree = spFactoryDeliverable;
        rdTreeDeliverables.ItemTemplate = dataTemplate;

to display nodes with Students' Names. But the problem is that- It will display the name of only single College's student names. It will not display the records of second or any further college record.

Any suggestion/idea?


{Binding Path=/} shows the current item from a list, it does not iterate over the list in any way - if you want to show nested lists you need some type of nested data templates (either HierarchicalDataTemplate or a DataTemplate with an ItemsControl in it - depending on the control this template will be used with).

You should also think about "flattening" the list in code before binding if you want to use a control that does not work with hierarchies.


Don't create templates in code behind, it's a major pain and prone to errors. Please provide the code for the implementation of your classes.

Your template is not tree-like in any way (your source is) so this wont do at all, you need to define nested templates for all levels or make the template recursive.

I do not think you can jump to a deep level directly either. It's a rather complex directive that requires the accumulation of all the subcollections into one.

{Binding Path=/} returns the current item, so if you want all names of a college you need to use the path Colleges/Students which returns the collection of students for the current college. If you need the names of all students in all colleges that will probably require nesting.

0

精彩评论

暂无评论...
验证码 换一张
取 消