UnderNotic
UnderNotic

Reputation: 355

XAML: Binding Collection inside of collection

I hava difficulties in binding to a collection of collection of treeviews using itemsSource. Eventually i wrote code liste below but it still doesn't work.

MainWindows.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="ButtonBase_OnClick"/>
            <TreeView Name="TreeView" VerticalAlignment="Top">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                        <ItemsControl ItemsSource="{Binding Tuples}" >
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Item1}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </StackPanel>
</Window>

And MainWindows.xaml.cs:

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

    public ObservableCollection<Person> Col { get; set; }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var Trades = new ObservableCollection<Person>
                         {   
                             new Person()
                                 {
                                     Tuples = new List<Tuple<string,string>>()
                                         {
                                             new Tuple<string, string>("Peter1", "Adam1"),
                                             new Tuple<string, string>("Peter2", "Adam2"),
                                             new Tuple<string, string>("Peter3", "Adam3")
                                         },
                                      Name = "MyName"
                                 },

                                  new Person()
                                 {
                                     Tuples = new List<Tuple<string,string>>()
                                         {
                                             new Tuple<string, string>("Peter1", "Adam1"),
                                             new Tuple<string, string>("Peter2", "Adam2"),
                                             new Tuple<string, string>("Peter3", "Adam3")
                                         },
                                         Name = "MyName2"
                                 }
                         };

        TreeView.ItemsSource = Trades; // Why it's not working ?
    }
}

public class Person
{
    public string Name { get; set; }
    public IEnumerable<Tuple<string, string>> Tuples;
}

Property Name of class Person is displayed correctly but Person's IEnumerable collection isn't displayed at all. What I'm doing wrong? Is there a better wa to do that? If possible i want to minimalize size of code on code-behind behalf.

Upvotes: 0

Views: 633

Answers (2)

Koopakiller
Koopakiller

Reputation: 2884

The problem, why Tuples are not shown in the gui, is that Tuples is not a property. It is a field. You need to change the declaration:

public IEnumerable<Tuple<string, string>> Tuples{get;set;}

But as I said in my comment, you should rethink about the use of a TreeView. A simple ListView do the same thing.

Upvotes: 1

James Harcourt
James Harcourt

Reputation: 6379

Ok, so you forgot the getter / setter on your "Tuples" property:

public IEnumerable<Tuple<string, string>> Tuples { get; set; }

This will now work, everything else is ok.

Upvotes: 0

Related Questions