我希望在某些文本框中更新电影信息,具体取决于所选的电影
我有
public Movie SelectedMovie { get; set; }
在我的viewmodel中,这是我的datacontext设置在
每当我的列表中选择一部电影时,它都会更新“SelectedMovie”
但只有第一部电影更新了文本块
<TextBlock Grid.ColumnSpan="2" Text="{Binding Path=SelectedMovie.Name}" FontSize="17" />
(应用程序加载时选择的那个)
所以当我在列表中选择一部新电影时,不确定为什么不改变文字?
电影课:
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);
}
}
}
答案 0 :(得分:4)
您必须在SelectedMovie属性的setter中触发PropertyChanged事件,以便通知绑定某些内容已更改:
private Movie selectedMovie;
public Movie SelectedMovie
{
get
{
return selectedMovie;
}
set
{
selectedMovie = value;
InvokePropertyChanged("SelectedMovie");
}
}