0

I need to show items from two different sources in my Tree View. How can I achieve that?

Details: Item source A: This item source will have a header, n children, where each child can have up to 32 sub children.

Item source B: This item source will have a header and n children.

I need to support context menu operations on the children and sub children.

What should the xaml code look like and what should be the structure of my classes?

My current structure has only one ItemSource and the binding code looks like this

<TreeView.ItemTemplate>
  <HierarchicalDataTemplate ItemsSource="{Binding Slash}">
         <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}"/>
         </StackPanel>
    <HierarchicalDataTemplate.ItemTemplate>
       <DataTemplate DataType="{x:Type dt:Slash}">
           <StackPanel Orientation="Horizontal">
               <TextBlock Text="{Binding FullName}"/>
           </StackPanel>
       </DataTemplate>
    </HierarchicalDataTemplate.ItemTemplate>
  </HierarchicalDataTemplate>
</TreeView.ItemTemplate>
WAQ
  • 2,556
  • 6
  • 45
  • 86

2 Answers2

0

You can take a look at the answer I posted in this topic If you have any question, just ask ;)

Community
  • 1
  • 1
Daniel
  • 9,312
  • 3
  • 48
  • 48
  • My problem is both Item Sources are of different type. So adding a category simply does not solve the issue – WAQ Oct 07 '13 at 08:27
  • Did you try to stack two treeviews? – Daniel Oct 07 '13 at 08:30
  • I cant do that. Requirement is to do this using one TV control only – WAQ Oct 07 '13 at 08:33
  • You cannot have two itemSources. So you will have to wrap the two itemsources in one object and manage it in the datatemplate that will create one TV for each sub list. Otherwise, you may have to use inheritance to have the same type for both sources. – Daniel Oct 07 '13 at 08:38
0

All you need to do is to put both of your collections into one 'ObservableCollectioncollection and thenBindthat to theTreeView.ItemsSource` property.

If for some reason, you need the collection to be typed, then you still have three options; either sub class the two types in the two collections and make your new collection of the sub type; or make them both implement the same interface and use that as the type of the new collection; or extend ObservableCollection to allow the collection to accept the two types of objects, while still checking their types at every input point.

Sheridan
  • 68,826
  • 24
  • 143
  • 183