[英]wpf databinding not updating from a property
I want movieinformation updated in a some textboxes depending on the selected movie 我希望在某些文本框中更新电影信息,具体取决于所选的电影
I have 我有
public Movie SelectedMovie { get; set; }
in my viewmodel which is what my datacontext is set at 在我的viewmodel中,这是我的datacontext设置的
And whenever a movie is selected in my list it updates "SelectedMovie" 每当我的列表中选择一部电影时,它都会更新“SelectedMovie”
But only the first movie updates the textblock 但只有第一部电影更新了文本块
<TextBlock Grid.ColumnSpan="2" Text="{Binding Path=SelectedMovie.Name}" FontSize="17" />
(The one selected when the application loads) (应用程序加载时选择的那个)
So not really sure why its not changing the text when I select a new movie in 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);
}
}
}
You have to fire PropertyChanged event in the setter of the SelectedMovie property in order to notify the binding that something has changed: 您必须在SelectedMovie属性的setter中触发PropertyChanged事件,以便通知绑定某些内容已更改:
private Movie selectedMovie;
public Movie SelectedMovie
{
get
{
return selectedMovie;
}
set
{
selectedMovie = value;
InvokePropertyChanged("SelectedMovie");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.