Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Complex Hierarchical data template

I'm looking at a complex structure and I can't seem to find a way to display it...

My situation:

Class: Milestone has 2 lists inside, a list of other submilestones and a list of activities.

So structured could be like:

M1

  • Milestones
    • SubMilestone
      • Milestones
      • Activities
    • Submilestone
  • Activities

Anybody has any idea's on how to create this? or could boost me into a direction?

ANSWER to my problem

<TreeView ItemsSource="{Binding Path=Project.TrackList}">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type deto:Track}" ItemsSource="{Binding Path=FaseList}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=TrackType}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type deto:Fase}" ItemsSource="{Binding Path=MilestoneList}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=FaseType}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type deto:Milestone}" ItemsSource="{Binding Converter={StaticResource MConverter}}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Description}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type deto:Activity}" ItemsSource="{Binding Path=ActivityList}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=Description}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>

And the converter:

public class MilestoneConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var m = value as Milestone;
        CompositeCollection collection = new CompositeCollection();
        collection.Add(new CollectionContainer()
        {
            Collection = m.MilestoneList
        });
        collection.Add(new CollectionContainer()
        {
            Collection = m.ActivityList
        });
        return collection;

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
like image 915
Tom Ceuppens Avatar asked Jan 19 '23 21:01

Tom Ceuppens


1 Answers

You should be able to do this using a CompositeCollection for example. Doing it in Xaml could be bit complicated in terms of referencing the sources, but using a converter should be acceptable in this case:

public class MilestoneItemsSourceCreator : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var input = value as Milestone;
        CompositeCollection collection = new CompositeCollection();
        collection.Add(new CollectionContainer(){ Collection = input.SubMilestones });
        collection.Add(new CollectionContainer(){ Collection = input.Activities });
        return collection;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
<vc:MilestoneItemsSourceCreator x:Key="MilestoneItemsSourceCreator"/>
<HierarchicalDataTemplate DataType="{x:Type local:Milestone}"
                          ItemsSource="{Binding Converter={StaticResource MilestoneItemsSourceCreator}}">
    <!-- DataTemplate -->
</HierarchicalDataTemplate>

This might not completely fit your class structures but you did not post those explicitly, some adjustments might be needed.

like image 97
H.B. Avatar answered Jan 31 '23 12:01

H.B.