如何创建一个带有不同UI控件的子节点的TreeView [英] How to create a TreeView with child nodes of different UI Controls
问题描述
在我的程序中,我有一个 treeView
,我想允许 comboBoxes
c $ c> textBlocks 。我的 treeView
以及它的节点是在MVVM风格中创建的。我知道如何创建 textBlock
子节点,但从未创建任何其他UI工具的子节点。
我的 treeview的
xaml:
TreeView ItemsSource ={Binding UserControl_DataModel.TreeViewViewModel.ObservableCollection< TreeViewDataModel>}DisplayMemberPath =DisplayName.Value... /
这是创建新节点并添加到 TreeView
(TreeViewViewModel):
private TreeViewDataModel createNewNode(string nodeName)
{
var newNode = new TreeViewDataModel()
{
DisplayName = nodeName
};
newNode.Children.Add(new TreeViewDataModel(){DisplayName = nodeName});
return newNode;
}
public void addNewLocNode(string nodeName)
{
TreeObservableCollection.Add(createNewNode(nodeName));
}
如何创建子节点 ComboBoxes
,同时仍然允许 textblock
儿童?这让我困惑,因为我看不到任何部分的代码,指定什么UI工具的孩子成了。如果您需要查看更多代码,请与我们联系。
(上一个问题关于这个相同的 treeView
如果你需要引用它。)
更新:
为了清楚起见,这是我的 treeView
节点应该是:
当我尝试这样的:
< HierarchicalDataTemplate DataType ={x:Type Data:Node}ItemsSource ={Binding Teams}>
< StackPanel>
< TextBlock Text ={Binding IndividualProperty}/>
< ComboBox ItemsSource ={Binding CollectionProperty}/>
< / StackPanel>
< / HierarchicalDataTemplate>
我得到这个设置(就像树的每个项目都有一个 > 请注意, > / p>
comboBox
不是 textBlock
节点的子节点,更像是与 textBlock
节点相同的级别。 ComboBox
是集合控件。因此,您需要在node对象中使用一个collection属性来绑定到 ComboBox.ItemsSource
。而'node'对象中的单个属性可能需要 HierarchicalDataTemplate
有点像这样: code>< HierarchicalDataTemplate DataType ={x:Type Data:Node}ItemsSource ={Binding Teams}>
< TextBlock Text ={Binding IndividualProperty}/>
< / HierarchicalDataTemplate>
您将需要这样一个:
< HierarchicalDataTemplate DataType ={x:Type Data:Node}ItemsSource ={Binding Teams}&
< ComboBox ItemsSource ={Binding CollectionProperty}/>
< / HierarchicalDataTemplate>
或者可能这样:
< HierarchicalDataTemplate DataType ={x:Type Data:Node}ItemsSource ={Binding Teams}&
< StackPanel>
< TextBlock Text ={Binding IndividualProperty}/>
< ComboBox ItemsSource ={Binding CollectionProperty}/>
< / StackPanel>
< / HierarchicalDataTemplate>
In my program I have a treeView
for which I want to allow child nodes that are comboBoxes
and textBlocks
. My treeView
along with it's nodes are created in an MVVM style. I know how to create textBlock
child nodes, but have never created child nodes of any other UI tool.
My treeview's
xaml:
TreeView ItemsSource="{Binding UserControl_DataModel.TreeViewViewModel.ObservableCollection<TreeViewDataModel>}" DisplayMemberPath="DisplayName.Value".../>
This is where new nodes are created and added to the TreeView
(TreeViewViewModel):
private TreeViewDataModel createNewNode(string nodeName)
{
var newNode = new TreeViewDataModel ()
{
DisplayName = nodeName
};
newNode.Children.Add(new TreeViewDataModel () { DisplayName = nodeName});
return newNode;
}
public void addNewLocNode(string nodeName)
{
TreeObservableCollection.Add(createNewNode(nodeName));
}
How do I create child nodes that are ComboBoxes
, while still allowing the textblock
children? This confuses me, because I don't see any part of the code that specifies what UI tool the children become. Please let me know if you need to see more code.
(Previous question about this same treeView
if you need to reference it.)
Update:
Just for clarity, this is how my treeView
nodes should look:
When I try something like this:
<HierarchicalDataTemplate DataType="{x:Type Data:Node}" ItemsSource="{Binding Teams}">
<StackPanel>
<TextBlock Text="{Binding IndividualProperty}" />
<ComboBox ItemsSource="{Binding CollectionProperty}" />
</StackPanel>
</HierarchicalDataTemplate>
I instead get this setup (it's like each item of the tree comes with a TextBlock
and a ComboBox
. Maybe there is another option besides StackPanel
?):
Note that the comboBox
is not a child of the textBlock
node, but it's more like it's on the same level as the textBlock
node.
A ComboBox
is a collection control. Therefore, you need a collection property in your 'node' object to bind to the ComboBox.ItemsSource
. While an individual property in the 'node' object might need a HierarchicalDataTemplate
somewhat like this:
<HierarchicalDataTemplate DataType="{x:Type Data:Node}" ItemsSource="{Binding Teams}">
<TextBlock Text="{Binding IndividualProperty}" />
</HierarchicalDataTemplate>
You would need one more like this:
<HierarchicalDataTemplate DataType="{x:Type Data:Node}" ItemsSource="{Binding Teams}">
<ComboBox ItemsSource="{Binding CollectionProperty}" />
</HierarchicalDataTemplate>
Or perhaps like this:
<HierarchicalDataTemplate DataType="{x:Type Data:Node}" ItemsSource="{Binding Teams}">
<StackPanel>
<TextBlock Text="{Binding IndividualProperty}" />
<ComboBox ItemsSource="{Binding CollectionProperty}" />
</StackPanel>
</HierarchicalDataTemplate>
这篇关于如何创建一个带有不同UI控件的子节点的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!