WPF:TreeView里面的ComboBox [英] WPF: TreeView inside a ComboBox

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

问题描述

我试图把一个TreeView在WPF内的ComboBox,所以当组合框被删除,而不是一个平面列表,用户获得一个分层列表,并且他们选择的任何节点成为ComboBox的选定值。



我已经搜索了一些如何完成这个,但最好的我可以找到只有潜在的解决方案,因为我是可笑的新的WPF,不能做工作。



我有足够的WPF和数据绑定的知识,我可以得到我的数据到树视图,我甚至可以在组合框里面的树视图,能够完成不正常的行为。我附上了一个截图来显示我的意思。在截图中,组合框是打开的,因此底部的树视图是我可以选择一个节点,并且在顶部的树视图正在组合框的顶部绘制,我想要所选节点的文本/值



基本上我不知道怎么做是我如何获得treeview的currrently选择的节点返回它的值回到combobox然后使用它作为其选择的值?



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

 < ComboBox Grid.Row =0Grid.Column =1VerticalAlignment =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>

屏幕截图:

解决方案

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


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:TreeView里面的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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