如何为WPF TreeView设置DataTemplate以显示列表的所有元素? [英] How do I set a DataTemplate for a WPF TreeView to display all Elements of an List?

查看:143
本文介绍了如何为WPF TreeView设置DataTemplate以显示列表的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用WPF中的TreeViews来可视化以下数据结构:

I'd like to visualize the following data structure using TreeViews in WPF:

class MyDataContext
{
    ICollectionView Outers {get;set;}
    //...
}

class Outer
{
    string Name {get;set;}
    IEnumberable<Inner> Actions {get;set;} 
}


class Inner
{
    string Description {get;set;}
    Command OnClick {get;set;}
}

这是我到目前为止的尝试:

This is my attempt so far:

<!-- DataContext is MyDataContext at this  point -->
<TreeView ItemsSource="{Binding Path=Outers}">
    <TreeView.Resources>
        <DataTemplate DataType="{x:Type myns:Outer}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}"/>

                <TreeView ItemsSource="{Binding Path=Actions}" >
                    <DataTemplate DataType="{x:Type myns:Inner}">
                        <Button Command={Binding Path=OnClick}>
                            <TextBlock Text="{Binding Path=Description}"/>
                        </Button>
                    </DataTemplate>
                </TreeView>
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

这个接口像这样的访问有问题,因为我收到以下 InvalidOperationException

It seams like there's something wrong with this access since I get the following InvalidOperationException:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

如果我放弃内部TreeView,那里也不例外(当然也没有按钮)。 >

If I drop the inner TreeView there's no exception (but also no buttons of course).

推荐答案

我使用了Mateusz提到的页面( HierarchicalDataTemplate ),并在阅读了这个问题的答案后:收集到StackPanel 我找到了一个解决方案:我想要的:

I used the page Mateusz mentioned (HierarchicalDataTemplate) and after reading the answer to this question: Bind Collection to StackPanel I found a solution that did what I wanted:

这里的玩家(3级)与队伍(第2级)位于同一行:

Here the players (level 3) are on the same row as the team (level 2):

<TreeView ItemsSource="{Binding League}">
    <!-- Conference template -->
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Teams}">
            <TextBlock Foreground="Red" Text="{Binding Name}" />
            <!-- Team template -->
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"/>
                        <ItemsControl ItemsSource="{Binding Players}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal">
                                    </StackPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Content="{Binding }"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这篇关于如何为WPF TreeView设置DataTemplate以显示列表的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆