使用数据绑定生成TreeView ??? [英] Generating a TreeView using databinding???

查看:102
本文介绍了使用数据绑定生成TreeView ???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我是第一次使用WPF进行项目,所以我不是WPF专家,并且TreeView存在一些问题.
我试图从我的类菜单的实例创建一个treeView.菜单由项组成,每个项可以有一个子项的集合,子项也可以有子项,等等...

我正在初始化菜单,并且一切正常,但是当我尝试使用dataBinding在treeView中显示菜单的内容时,我遇到了很多问题,并且我正在寻找解决方案,但没有找到任何东西.

这是我的xaml代码:

Hi everybody.
I am doing a project with WPF, it''s the first time, so i am not an expert in WPF, and i have some problems with treeView.
I am trying to create a treeView from an instance of my class menu. A menu is composed by Items and every Item can have a collection of subItems, which can also have subItems and so on...

I am initializing my menu and everything is right, but when i try to use the dataBinding to show the content of my menu in a treeView i have many problems, and i am searching for a solution, but didn''t find anything.

Here is my xaml code:

<Grid.Resources>
            <HierarchicalDataTemplate x:Key="Temp">
                <TreeViewItem  ItemsSource="{Binding Path=ViewModelPropertyMenu.it/Subitems, Source={StaticResource Locator}}" Header="{Binding Path=ViewModelPropertyMenu.it/Label, Source={StaticResource Locator}}"  />
            </HierarchicalDataTemplate>

        </Grid.Resources>


....

<pre lang="xml"><TreeView  AllowDrop="True"  >

                <TreeViewItem Header="{Binding Path=Name}" ItemTemplate="{StaticResource Temp}" ItemsSource="{Binding Path=it, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />


            </TreeView>




我正在使用模式MVVM和Locator,因此该类定位器用于查找我正在使用的ViewModel类.

生成的treeView显示三个具有相同名称的项目(第一个项目的名称,并且我的菜单实际上由三个项目组成),然后每个项目显示11个子项目(只有我的第一个项目具有11个子项目),但显示它们以这种形式"Project.Model.Items"

提前致谢.我真的迷路了.自从我寻找解决方案以来已经过去了一周. :sigh:




I am using the patterns MVVM and Locator, so The class locator is used to locate the ViewModel Classes i am using.

The treeView generated shows three items which have the same name (the name of the first item, and my menu is really composed of three items) then for every Item, it shows 11 subItems ( only my first Item have 11 SubItems) but it shows them under this form "Project.Model.Items"

Thanks in Advance. I am really lost. It has been a week since i am searching for a solution. :sigh:

推荐答案

我正在使用Telerik控件,并且遇到相同的问题.在 http://blogs上找到了将适当的ValueConverter应用于某些项目集合的棘手解决方案. telerik.com/valerihristov/posts/09-08-26/self-reference_hierarchy_with_telerik_treeview_for_silverlight.aspx .我不知道此解决方案是否可以与常见的TreeView一起使用.大多数事情看起来都差不多,所以也许您可以找到有用的东西.
I''m working with Telerik controls and had the same problem. Found tricky solution with proper ValueConverter applied to certain items collection at http://blogs.telerik.com/valerihristov/posts/09-08-26/self-reference_hierarchy_with_telerik_treeview_for_silverlight.aspx. I don''t know if this solution can be used with common TreeView. Most things looks similar so maybe you can find something usefull.


感谢helianthus87的回复,但对我来说却不一样.
Thanks helianthus87 for responding, but it is not the same for me.


我发现了为什么我对每个商品都使用相同的名称.实际上,我为HierarchicalDataTemplate和TreeView使用了不同的DataContext.
现在,它向我显示了项目的真实名称.
但是,我仍然有两个问题:
1)子项目仍处于此形式(Project.Model.Items)
2)如何声明我的HierarchicalDataTemplate具有项目的所有子项,直到没有更多子项为止.
I discovered why i had the same name for every Item. In fact i was using different DataContext for the HierarchicalDataTemplate and the TreeView.
Now it shows me the real names of my items.
But, i still have two problems:
1) The subitems still under this form (Project.Model.Items)
2) How i should declare my HierarchicalDataTemplate to have all the subitems of my items until there is no more subitems.


这篇关于使用数据绑定生成TreeView ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆