0

I am trying to create a TreeView that allows child nodes of types textBlock and comboBox. Here is a picture of what this would look like:

enter image description here

I believe that this is an issue that can be solved by using a HierarchicalDataTemplate because the xaml is the area of the code that I would specify what UI control I cam using. So far I have tried implementing a StackPanel with my HierarchicalDataTemplate like so:

<HierarchicalDataTemplate DataType="{x:Type data:DataModel}" ItemsSource="{Binding Children}">
    <StackPanel>
        <TextBlock Text="{Binding DisplayName}" />
        <ComboBox ItemsSource="{Binding CommandCollection}" />
    </StackPanel>
</HierarchicalDataTemplate>

But I do not achieve the correct solution with this because StackPanel is basically setting up each node so that they contain both a textBlock and a comboBox. This is a problem because each child node is either a textBlock or a comboBox, never both. How do I set up a HierarchicalDataTemplate that allows TreeView child nodes to be either textBlocks or comboBoxes? Please let me know if you would like more details on how my TreeView is implemented, or would like to see more code.

Some background references to this question can be found here, and here.

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

0

Usually it is a good idea to template different types of data with respective data templates. This can be achieved implicitly by placing the templates in the TreeView.Resources and using the DataTemplate.DataType property.

Here is an example: [Link]

The only thing that differs is that you would use a hierarchical template and bind the ItemsSource of the template respectively to a property containing child items.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • The DataType that I'm using right now is the Data Model of my `TreeView`. I understand that when a `TextBlock` is displayed it is displaying usually a `string`, whereas a `ComboBox` would display an `ObservableCollection` in most cases. Therefore, I do not understand what I would use instead of my Data Model. – Eric after dark Jul 15 '14 at 17:25
  • 1
    @Ericafterdark: Use custom classes, that contain the necessary information, e.g. the `StringTreeItem` would contain a string property, and `ComboTreeItem` would contain one property for the selected value and another containing the list of possible items. If you only have one type right now you will need to map your structure to at tree using those respective types based on whatever criterion you have (if there is none you have a problem in any case). – H.B. Jul 15 '14 at 18:27