Wpf data binding not updating from property

I want to update the movie information in some text blocks depending on the selected movie

I have

public Movie SelectedMovie { get; set; } 

in my viewmodel model, which is what my datacontext is set to

And whenever a movie is selected on my list, it updates "SelectedMovie"

But only the first movie updates the text block

<TextBlock Grid.ColumnSpan="2" Text="{Binding Path=SelectedMovie.Name}" FontSize="17" />

(the one that is selected when downloading the application)

So it’s not entirely clear why he doesn’t change the text when I select a new movie from the list?

Movie Class:

using System;
using System.ComponentModel;

namespace MovieDB3.Models
{
    class Movie : INotifyPropertyChanged
    {
        public Movie(string name)
        {
            this.name = name;
        }

        private string name;
        public string Name 
        {
            get { return name; }
            set
            {
                name = value;
                InvokePropertyChanged("Name");
            }
        }
        public int Id { get; set; }
        private double rating;
        public double Rating
        {
            get { return rating; }
            set 
            { 
                rating = value;
                InvokePropertyChanged("Rating");
            }
        }

        public DateTime Release { get; set; }
        public TimeSpan Runtime { get; set; }
        public String Trailer { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(String propertyName)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
            PropertyChangedEventHandler changed = PropertyChanged;

            if (changed != null) changed(this, e);
        }
    }
}
+3
source share
1 answer

You must fire the PropertyChanged event in the installer of the SelectedMovie property to notify the binding that something has changed:

private Movie selectedMovie;

public Movie SelectedMovie
{
    get
    {
        return selectedMovie;
    }
    set
    {
        selectedMovie = value;
        InvokePropertyChanged("SelectedMovie");
    }
}
+4
source

Source: https://habr.com/ru/post/1789522/


All Articles