maxeber
maxeber

Reputation: 71

How to have multiple level of binding with ListViews

Here's my problem, I want to have a few ListViews with their ItemsSource attached with binding to SelectedItem of other ListViews.

Here's a picture to show you the interface to give you an overlook : enter image description here

So I have a "Tours" ListView who's ItemSource is bonded with a ObservableCollection property.

Now I want the "Parties" ListView to be bond with the SelectedItem of the "Tours" ListView.

Afterwards, I want the "Équipes" ListView to be bound with the SelectedItem of the "Parties" ListView.

And so on...

Right now, it is working, but the program crash :

  1. Select a "Tour" from "Tours"
  2. Select a "Partie" from "Parties"
  3. Select a "Équipe" from "Équipes"
  4. Select a "Different "Tour" from "Tours"
  5. Crash

I supposed it was when I change "Tour", the "Équipe" point toward something inexistant.

This is the XAML of the ListViews and the content (the content isn't bond right now) :

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.2*" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel>

            <Label Style="{StaticResource menu}" Content="Tours" />
            <ListView Name="lvTours" ItemsSource="{Binding Tours}" SelectionChanged="lvTours_SelectionChanged">
                <ItemsControl.ItemTemplate>

                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>

                </ItemsControl.ItemTemplate>
            </ListView>

            <Label Style="{StaticResource menu}" Content="Parties" />
            <ListView Name="lvParties" DataContext="{Binding RelativeSource={RelativeSource Self}}" SelectionChanged="lvParties_SelectionChanged" >
                <ItemsControl.ItemTemplate>

                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>

                </ItemsControl.ItemTemplate>
            </ListView>

            <Label Style="{StaticResource menu}" Content="Équipes" />
            <ListView Name="lvEquipes" DataContext="{Binding RelativeSource={RelativeSource Self}}">
                <ItemsControl.ItemTemplate>

                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>

                </ItemsControl.ItemTemplate>
            </ListView>
        </StackPanel>

        <Label Content="Here's the content..." Grid.Column="1" Margin="30" />

    </Grid>

And here's the cs :

using Lama.Logic.Model.Test;
using System.Windows.Controls;

namespace Lama.UI.UC.TournoiControls.StatistiquesControls
{
    /// <summary>
    /// Interaction logic for Statistiques.xaml
    /// </summary>
    public partial class StatistiquesUC : UserControl
    {
        public Tour SelectedTour { get; set; }
        public Partie SelectedPartie { get; set; }

        public StatistiquesUC()
        {
            InitializeComponent();
        }

        private void lvTours_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lvParties.ItemsSource = ((Tour)lvTours.SelectedItem).Parties;
        }

        private void lvParties_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lvEquipes.ItemsSource = ((Partie)lvParties.SelectedItem).Equipes;
        }
    }
}

So :

Thanks and sorry for my english!

Upvotes: 0

Views: 933

Answers (1)

user5447154
user5447154

Reputation:

Since you dont want a ViewModel, this is an other approche :

In your xaml does not need to SelectionChanged, just bind itemSource of Lisview Equipe & Partie the parent list view like this :

<Grid DataContext="{Binding YourViewModel}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.2*" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel>

            <Label Style="{StaticResource menu}" Content="Tours" />
            <ListView Name="lvTours" ItemsSource="{Binding Tours}" >
                <ItemsControl.ItemTemplate>

                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>

                </ItemsControl.ItemTemplate>
            </ListView>

            <Label Style="{StaticResource menu}" Content="Parties" />
            <ListView Name="lvParties" ItemsSource="{Binding ElementName=lvTours, Path=SelectedItem.Parties}" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListView>

            <Label Style="{StaticResource menu}" Content="Équipes" />
            <ListView Name="lvEquipes"  ItemsSource="{Binding ElementName=lvParties, Path=SelectedItem.Equipes}" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Nom}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListView>
        </StackPanel>

        <Label Content="Here's the content..." Grid.Column="1" Margin="30" />

    </Grid>

Hope it helps

Upvotes: 1

Related Questions