WPF手动生成的TreeViewItem容器 [英] wpf manually generate TreeViewItem container

查看:479
本文介绍了WPF手动生成的TreeViewItem容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时创建一个TreeView。它具有若干节点(树型视图),每一个具有一个名称。最初它是折叠的。一个单独的下拉框中显示所有的TreeViewItem的名称。我有一个基于选择的名称要突出一个树型视图。我使用的是递归函数,并得到的TreeViewItem容器是这样的:

I am creating a TreeView at runtime. It has several nodes(TreeViewItem), each one having a name. Initially it is collapsed. A separate comboBox displays Names of all TreeViewItem. I have to highlight a TreeViewItem based on the Name selected. I am using a recursive function and gets the TreeViewItem container like this:

if (parent.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                continue;

TreeViewItem container = parent.ItemContainerGenerator.ContainerFromItem(child).As<TreeViewItem>();



but it is

parent.ItemContainerGenerator.Status = GeneratorStatus.NotStarted

parent.ItemContainerGenerator.Status = GeneratorStatus.NotStarted

所有倒塌的物品。我如何可以手动生成它们的容器(不扩大他们)?

for all the collapsed items. How can I generate containers for them manually(Without expanding them)?

推荐答案

每当你发现自己走可视化树,你应该想到认真思考建立一个视图模型。

Anytime you find yourself walking the visual tree you should think seriously about building a view model.

视图模型可以提高树视图功能,无需担心各种你所面对的问题。

the view model can increase the treeviews functionality without worrying about the kinds of issues you are facing.

为你的应用程序视图模型将有一对夫妇的额外属性。

The view model for your application would have a couple of extra properties

IsSelected

IsExpanded

IsHighlighted

,以及你的

Data

那么你会绑定(使用ItemContainerStyle )的IsSelected,IsHighlighted和IsEx​​panded到树型视图,那么你有这些视图模型的集合,你可以做你的视图模型搜索和刚才设置的属性IsHighlighted。然后,视图模型将作出回应,因为结合的风格,如果你想它会突出树型视图。它也可以做时髦的东西像上面扩大所有节点。树视图将对此作出回应。

then you would bind (using an ItemContainerStyle) the IsSelected, IsHighlighted and IsExpanded to the TreeViewItem, then as you had a collection of these view models you could do your search on the view model and just set the property IsHighlighted. The view model would then respond and because of the binding to the style it would highlight the treeviewitem if you wanted. It could also do funky stuff like expand all the nodes above. The tree view would respond to this.

在一个树视图结合视图模型提供了更大的灵活性,并停止这些可视树爬行运动(这是一个黑客,和感觉脏)

The view model when combined with a treeview gives much more flexibility and stops those visual tree crawling exercises (which are a hack, and feel dirty)

这里是一个好文章对视图模型和TreeView控件 ...

这篇关于WPF手动生成的TreeViewItem容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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