如何为WPF TreeView设置DataTemplate以显示列表的所有元素?

时间:2013-07-19 08:56:02

标签: c# wpf data-binding treeview datatemplate

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

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

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

<!-- 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,因此这种访问出现了问题:

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

如果我删除内部TreeView,则没有例外(当然也没有按钮)。

3 个答案:

答案 0 :(得分:6)

我使用了Mateusz提到的页面(HierarchicalDataTemplate)并在阅读了这个问题的答案之后:Bind Collection to StackPanel我找到了一个能够达到我想要的解决方案:

这里的球员(3级)与球队(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>

the result

答案 1 :(得分:1)

请尝试将HierarchicalDataTemplate与TreeView一起使用。

HierarchicalDataTemplate

答案 2 :(得分:0)

也许这对你有所帮助;)

xaml:

    <TreeView ItemsSource="{Binding Outers}">
        <TreeView.ItemTemplate>
            <DataTemplate>
                <TreeViewItem ItemsSource="{Binding Actions}" Header="{Binding Name}">
                    <TreeViewItem.ItemTemplate>
                        <DataTemplate>
                            <Button Command="{Binding Click}" Content="{Binding Name}" />
                        </DataTemplate>
                    </TreeViewItem.ItemTemplate>
                </TreeViewItem>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

数据:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MyDataContext();
    }
}



    class MyDataContext
    {
        public ObservableCollection<Outer> Outers { get; set; }


        public MyDataContext()
        {
            Outers = new ObservableCollection<Outer>();
            Outers.Add(new Outer() { Name = "Herp" });
            Outers.Add(new Outer() { Name = "Derp" });
        }
    }

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

        public Outer()
        {
            Actions = new ObservableCollection<Inner>();
            Actions.Add(new Inner { Name = "Test1" });
            Actions.Add(new Inner { Name = "Test2" });
            Actions.Add(new Inner { Name = "Test3" });
            Actions.Add(new Inner { Name = "Test4" });
            Actions.Add(new Inner { Name = "Test5" });
            Actions.Add(new Inner { Name = "Test6" });
            Actions.Add(new Inner { Name = "Test7" });
        }
    }


    class Inner
    {
        public string Name { get; set; }
        public ICommand OnClick { get; set; }
    }

如果您使用的是命令... 试试这个例子: ICommand