简体   繁体   English

WPF复杂分层数据模板

[英]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. 类:里程碑里面有2个列表,其他子里程碑列表和活动列表。

So structured could be like: 如此结构化可能像:

M1 M1

  • Milestones 里程碑
    • SubMilestone SubMilestone
      • Milestones 里程碑
      • Activities 活动
    • Submilestone 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
}

You should be able to do this using a CompositeCollection for example. 例如,您应该能够使用CompositeCollection执行此操作。 Doing it in Xaml could be bit complicated in terms of referencing the sources, but using a converter should be acceptable in this case: 在Xaml中执行它在引用源方面可能有点复杂,但在这种情况下使用转换器应该是可接受的:

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. 您应该在另一个上方显示两个列表控件(例如ListView / ListBox),并将第二个数据绑定到第一个选定项目。

In your case, it seems you'd need 3 list controls, adapt as you please. 在您的情况下,似乎您需要3个列表控件,随意适应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM