4
votes

I want to display the following using a wpf TreeView:

TreeView structure

My objects are different, there is no Base class or Interface, I must define a HierarchicalDataTemplate for each item, STOP for example I can add just one ItemSource "Deliveries" but I want to add the pickups also for this stop.

<!-- DELIVERY-->
<DataTemplate x:Key="DeliveryDataTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="DeliveryId"  Margin="3,3" />
        <TextBlock Text="{Binding DeliveryStatus}" VerticalAlignment="Center" Margin="5" />
        <TextBlock Background="{Binding StopStatus, Converter={StaticResource StatusConverter}}" Width="16" Height="16" />
    </StackPanel>
</DataTemplate>

<!-- STOP -->
<HierarchicalDataTemplate x:Key="StopTemplate"
                          ItemsSource="{Binding Deliveries}"
                          ItemTemplate="{StaticResource DeliveryTemplate}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Stop"  Margin="3,3" />
        <TextBlock Text="{Binding StopId}" Margin="3,3" />
        <TextBlock Background="{Binding StopStatus, Converter={StaticResource StatusConverter}}" Width="16" Height="16" Margin="3,3"  />
    </StackPanel>
</HierarchicalDataTemplate>

<!-- ROUTE -->
<HierarchicalDataTemplate x:Key="RouteTemplate"
                          ItemsSource="{Binding Stops}"
                          ItemTemplate="{StaticResource StopTemplate}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Route"  Margin="5,5" />
        <TextBlock Text="{Binding RouteId}" Margin="5,5" />
        <TextBlock Background="{Binding RouteStatus, Converter={StaticResource StatusConverter}}" Width="16" Height="16"  Margin="5,5" />
    </StackPanel>
</HierarchicalDataTemplate>

I have a collection of Routes, each Route has Stops, each Stop has Deliveries and Pickups, each Delivery has its items each item has its own items and so on... How to solve this?

1
I think fallow like solution is so good [WPF Treeview Databinding Hierarchal Data with mixed types][question]. [question]:stackoverflow.com/questions/3673173/… - ebadola sobhanwerdy

1 Answers

3
votes

This sounds like a heterogenous datasource problem. I think this solution might be what you are looking for.