0
votes

I have a ListBox within a DataTemplate of another ListBox simplified to the following XAML

<ListBox ItemsSource="{Binding MovieList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <ListBox ItemsSource="{Binding Cast}" 
                         DisplayMemberPath="Name"
                         SelectedItem="{Binding SelectedCastMember, Mode=TwoWay}"/>
                <TextBlock Text="{Binding MovieName}"/>
                <TextBlock Text=....../>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The Parent List is bound to an ObservableCollection of Movie with each Movie object in turn having an ObservableCollection of cast members bound to a list box. The ViewModel property and Class below

public const string MovieListPropertyName = "MovieList";

    private ObservableCollection<Movie> _movieList;

    public ObservableCollection<Movie> MovieList
    {
        get
        {
            return _movieList;
        }

        set
        {
            if (_movieList == value)
            {
                return;
            }

            RaisePropertyChanging(MovieListPropertyName);
            _movieList = value;
            RaisePropertyChanged(MovieListPropertyName);
        }
    }

public const string SelectedCastMemberPropertyName = "SelectedCastMember";

    private MovieCastMember _selectedCastMember;

    public MovieCastMember SelectedCastMember
    {
        get
        {
            return _selectedCastMember;
        }

        set
        {
            if (_selectedCastMember == value)
            {
                return;
            }

            RaisePropertyChanging(SelectedCastMemberPropertyName);
            _selectedCastMember = value;
            RaisePropertyChanged(SelectedCastMemberPropertyName);
        }
    }

With the Movie and MovieCastMember classes as follows

    public class Movie
    {                    
        public int Id { get; set; }
        public string Name { get; set; }
        public int Year { get; set; }
        public string Overview { get; set; }
        public double VoteAverage { get; set; }
        public ObservableCollection<MovieCastMember> Cast { get; set; }
        public BitmapImage PosterImage { get; set; }       
    }

    public class MovieCastMember
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

I want to select a cast member in any of the Movie Lists and bind the MovieCastMember object to a property in my ViewModel. My List boxes populates fine, I have tried various scenarios in XAML but the SelectedItem are not updating the property in the ViewModel. Any help would be appreciated.

1

1 Answers

0
votes

That's because you're binding against Movie class in second listBox. There is no "SelectedCastMember". Move it to Movie class and it will work.