开发者

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 Mileston开发者_如何学运维eConverter : 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
}


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.


I've done something similar in the past.

You should display two list controls (ListView/ListBox for example) one above the other one, and bind the second's Data to the first one's selected item.

In your case, it seems you'd need 3 list controls, adapt as you please.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜