Question

I have a parent ListBox which is made of two listboxes, and a child listbox, so 3 listboxes in total. The Item source for the first two are just two separate collections, these collections contain inner collection which then become an item source for the third listbox. If I select second item then the child listbox is populated again, if I go to the second parent listbox and chose something then the child listbox is populated. The problem is that if at this point I go back to the first listbox, and try to select an item that was previously selected, nothing happens, while im expecting it to populate the child listbox again.

Hopefully this makes sense, here is the XAML:

    <Window x:Class="ExampleForStackOverFlow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="350" Height="350">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <StackPanel>
        <TextBlock Text="Parents" FontSize="20" FontWeight="Bold" Background="LightBlue"/>
        <TextBlock Text="First Listbox" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Parents1}" SelectedItem="{Binding SelectedParent}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock Text="Second Listbox" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Parents2}" SelectedItem="{Binding SelectedParent2}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
    <StackPanel Grid.Column="1">
        <TextBlock Text="Children" FontSize="16" FontWeight="Bold" Background="LightBlue"/>
        <ListBox ItemsSource="{Binding Children}" BorderThickness="0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

This is the ViewModel

    using System.Collections.ObjectModel;

    namespace ExampleForStackOverFlow
    {
  public class MainWindowViewModel : NotifyPropertyChanged
   {
    public MainWindowViewModel()
    {
        Parents1 = new ObservableCollection<Parent>();
        Parents2 = new ObservableCollection<Parent>();
        foreach (var parent in ParentCollection.GetParents())
        {
            if (parent.Name == "Parent 1" || parent.Name == "Parent 2")
            {
                Parents1.Add(parent);
            }
            else
            {
                Parents2.Add(parent);
            }
        }
        Children = new ObservableCollection<Child>();
    }

    public ObservableCollection<Parent> Parents1 { get; set; }
    public ObservableCollection<Parent> Parents2 { get; set; }

    private ObservableCollection<Child> children;
    public ObservableCollection<Child> Children 
    {
        get { return children; }
        set
        {
            children = value;
            OnPropertyChanged("Children");
        }
    }

    private Parent selectedParent;
    public Parent SelectedParent
    {
        get { return selectedParent; }
        set 
        {
            selectedParent = value;
            Children = SelectedParent.Children;
            OnPropertyChanged("SelectedParent");
        }
    }

    private Parent selectedParent2;
    public Parent SelectedParent2
    {
        get { return selectedParent2; }
        set
        {
            selectedParent2 = value;
            Children = SelectedParent2.Children;
            OnPropertyChanged("SelectedParent2");
        }
    }
}

public class Parent : NotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private ObservableCollection<Child> childres;
    public ObservableCollection<Child> Children
    {
        get { return childres; }
        set 
        {
            childres = value;
            OnPropertyChanged("Children");
        }
    }
}

public class Child : NotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }
}

}

Was it helpful?

Solution

The value for selected did not change so it is not going to fire the set
If you set it to null then a re-select will be a new value

    private Parent selectedParent;
    public Parent SelectedParent
    {
        get { return selectedParent; }
        set 
        {
            selectedParent2 = null;
            selectedParent = value;
            Children = SelectedParent.Children;
            OnPropertyChanged("SelectedParent");
            OnPropertyChanged("SelectedParent2");  // you may not need this
        }
    }

    private Parent selectedParent2;
    public Parent SelectedParent2
    {
        get { return selectedParent2; }
        set
        {
            selectedParent = null;
            selectedParent2 = value;
            Children = SelectedParent2.Children;
            OnPropertyChanged("SelectedParent");  // you may not need this
            OnPropertyChanged("SelectedParent2");
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top