WPF 复杂分层数据模板

发布于 2024-11-03 19:36:14 字数 2832 浏览 4 评论 0原文

我正在查看一个复杂的结构,但似乎找不到显示它的方法...

我的情况:

类:里程碑里面有 2 个列表,一个其他子里程碑的列表和一个活动列表。

结构可能类似于:

M1

  • 里程碑
    • 子里程碑
      • 里程碑
      • 活动
    • 子里程碑
  • 活动

有人知道如何创建此活动吗?或者可以推动我走向一个方向?

回答我的问题

<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>

转换器:

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够钟 2024-11-10 19:36:14

您应该能够使用 CompositeCollection 来执行此操作例如。在引用源方面,在 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>

这可能不完全适合您的类结构,但您没有明确发布这些结构,可能需要进行一些调整。

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.

海风掠过北极光 2024-11-10 19:36:14

我过去也做过类似的事情。

您应该在另一个上方显示两个列表控件(例如 ListView/ListBox),并将第二个的数据绑定到第一个的所选项目。

就您而言,您似乎需要 3 个列表控件,请根据需要进行调整。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文