WPF:ComboBox 内的 TreeView [英] WPF: TreeView inside a ComboBox

查看:30
本文介绍了WPF:ComboBox 内的 TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 TreeView 放在 WPF 中的 ComboBox 中,这样当组合框被放置时,用户会得到一个分层列表而不是平面列表,并且他们选择的任何节点都成为 ComboBox 的选定值.

我已经搜索了很多如何实现这一点,但我能找到的最好的只是一些潜在的解决方案,因为我对 WPF 非常陌生,所以我无法工作.

我对 WPF 和数据绑定有足够的了解,可以将我的数据放入树视图中,甚至可以在组合框内获取树视图,但是我能够完成的操作根本无法正常运行.我附上了一张截图来说明我的意思.在屏幕截图中,组合框是打开的",所以底部的树视图是我可以选择一个节点的地方,而顶部"的树视图被绘制在组合框的顶部,我想要所选节点的文本/值在要显示的树中.

基本上我不知道该怎么做是如何让树视图当前选择的节点将其值返回到组合框,然后组合框将其用作其选定值?

这是我目前使用的 xaml 代码:

 <ComboBoxItem><TreeView ItemsSource="{Binding Children}" x:Name="TheTree"><TreeView.Resources><HierarchicalDataTemplate DataType="{x:Type Core:LookupGroupItem}" ItemsSource="{Binding Children}"><TextBlock Text="{绑定路径=显示}"/></HierarchicalDataTemplate></TreeView.Resources></树视图></ComboBoxItem></组合框>

截图:

解决方案

对于那些仍然需要这个控件的人,我已经实现了我的

I'm trying to put a TreeView inside a ComboBox in WPF so that when the combo box is dropped, instead of a flat list the user gets a hierarchical list and whatever node they select becomes the selected value of the ComboBox.

I've searched quite a bit for how to accomplish this but the best I could find was only peices of potential soltuions that, because I'm ridiculously new to WPF, I couldn't make work.

I have enough knowledge of WPF and databinding that I can get my data into the treeview and I can even get the treeview inside of the combo box, however what I've been able to accomplish doesn't behave properly at all. I've attached a screenshot to show what I mean. In the screenshot the combo box is "open", so the treeview on the bottom is where I can select a node and the treeview "on top" is being drawn on top of the combobox where I want the text/value of the selected node in the tree to be displayed.

Basically what I don't know how to do is how do I get the treeview's currrently selected node to return its value back up to the combobox which then uses it as its selected value?

Here is the xaml code I'm currently using:

        <ComboBox Grid.Row="0" Grid.Column="1"  VerticalAlignment="Top">
        <ComboBoxItem>
            <TreeView ItemsSource="{Binding Children}" x:Name="TheTree">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type Core:LookupGroupItem}" ItemsSource="{Binding Children}">
                        <TextBlock Text="{Binding Path=Display}"/>                            
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
        </ComboBoxItem>
    </ComboBox>

Screenshot:

解决方案

For those who still need this control, I've implemented a WPF version of my Silverlight control. It works only with view models and requires these view models to implement a special interface, but apart of this it's not difficult to use.

In WPF it looks like this:

You can download source code and sample application from here: WpfComboboxTreeview.zip

这篇关于WPF:ComboBox 内的 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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