实现INotifyPropertyChanged时,导航属性是否也需要实现它?

时间:2018-03-21 01:08:16

标签: c# inotifypropertychanged

在实施INotifyPropertyChanged(使用Prisim)时,下面的代码是有意义的,您想知道属性何时发生变化。

[DisplayName("Media Type Id"), Display(Name = "Media Type Id")]
public int MediaTypeId
{
    get { return this._MediaTypeId; }
    set { this.SetProperty(ref this._MediaTypeId, value); }
}
private int _MediaTypeId;

但是当谈到导航属性时我很困惑。

我实施它吗?对我而言,如果我要做artist.Album = new Album();之类的事情,那就更有意义了。 但是,如果仅需要更改artist.Album.name = "NEW_NAME"之类的属性(假设Album.name实现INotifyPropertyChanged),该怎么办? 下面的代码是否仍然有必要?

[DisplayName("Album"), Display(Name = "Album")]
public Album Album
{
    get { return this._Album; }
    set { this.SetProperty(ref this._Album, value); }
}
private Album _Album;

或者这项工作也一样

public virtual Album Album { get; set; }

导航集合相同。

[DisplayName("Playlists"), Display(Name = "Playlists")]
public ICollection<Playlist> Playlists
{
    get { return this._Playlists; }
    set { this.SetProperty(ref this._Playlists, value); }
}
private ICollection<Playlist> _Playlists

public virtual ICollection<Playlist> Playlists { get; set; }

1 个答案:

答案 0 :(得分:1)

根据您的理解,您实现了INotifyPropertyChanged(INPC),以便在模型上的属性更改时更新UI。因此,在您的情况下,如果您有一些数据绑定到Album属性,它必须实现INPC,如果它可能会更改。您没有使用常规集合,而是有一个名为ObservableCollection的类,它已经为您实现了INPC,因此您不必这样做。

相关问题