How do I set a DataTemplate for a WPF TreeView to display all elements of a list?
Asked Answered
D

3

5

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

It seems 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.

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

Davao answered 19/7, 2013 at 8:56 Comment(0)
D
7

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:

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>

the result

Davao answered 22/7, 2013 at 12:42 Comment(0)
M
1

Please try to use HierarchicalDataTemplate with TreeView.

HierarchicalDataTemplate

Microsporangium answered 19/7, 2013 at 9:0 Comment(1)
I had a look at the website you mentioned but still don't know how I get the actions as button on the same row as the "Outer" element. The example presented on this page would put each button on a new line below the "Outer" element.Davao
L
0

Maybe this helps you a little bit ;)

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

The data:

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

And if you are using Commands... Try it with this example: ICommand

Lingerie answered 19/7, 2013 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.