RaisePropertyChanged没有更新UI

时间:2011-08-29 23:35:09

标签: c# wpf windows-phone-7 mvvm-light windows-phone

当我的ViewModel发生变化时,我无法让我的UI更新两个列表框,以便正确更新。

首先,页面背后的基本逻辑:

电影是一个带有标题和各种MovieDetails的对象。有些MovieDetail与其他ListBoxes不同,因为它们是详细的,这是一种美化的方式,说它们更重要。 我使用两个MovieDetails将这些ListBoxes分隔为堆叠 BooleanToVisibilityConverter,一个用于“详细”,一个用于“未详细信息” 如果电影没有“详细”属性,则通过Movie隐藏相应的列表(反之亦然)

当我导航到该页面时,我设置了该页面对应的RaisePropertyChanged,并且应该AllMoviesDetail提醒ObservableCollection Movies.MovieDetailFetchedList它应该重新获取AllMoviesDetail。 从那里开始,ObservableCollections (Detailed, NotDetailed)会提醒两个get他们应该重新RaisePropertyChanged

事实上,NotDetailedMovieDetailsDetailedMovieDetails上的HasNotDetailedMovieDetails, Has...似乎也没有做任何事情。 (以及相应的ObservableCollections

但是,如果我在列表中添加更多项目,那么可以解决的问题是,CollectionChanged事件似乎触发并重新激活列表。我也可以通过首先在代码var temp = DetailedMoviesDetail;

中实例化public class MoviesDetailViewModel : ViewModelBase { #region Property Names public const string MoviePropertyString = "Movie"; public const string AllMoviesDetailPropertyString = "AllMoviesDetail"; public const string DetailedMoviesDetailPropertyString = "DetailedMoviesDetail"; public const string NotDetailedMoviesDetailPropertyString = "NotDetailedMoviesDetail"; public const string HasNotDetailedMoviesDetailPropertyString = "HasNotDetailedMoviesDetail"; public const string HasDetailedMoviesDetailPropertyString = "HasDetailedMoviesDetail"; public const string NotDetailedHeaderPropertyString = "NotDetailedHeader"; #endregion public MoviesDetailViewModel() { if (IsInDesignMode) { Movie = DesignDataStore.MovieList[0]; Movie.Category = Category.DDA; } } private Movie _Movie; /// <summary> /// The Movie for which to browse MoviesDetail. It is expected when setting this property, that MoviesDetail for it have been downloaded previously. /// </summary> /// <remarks>The 'Master' property for this ViewModel. All properties are Dependent on this and the underlying property MoviesDetailList</remarks> /// <seealso cref="MovieDetailFetchedList"/> public Movie Movie { get { return _Movie; } set { if (_Movie != value) { if (_Movie != null) _Movie.MovieDetailFetchedList.CollectionChanged -= MoviesDetailListChanged; _Movie = value; RaisePropertyChanged(MoviePropertyString); RaisePropertyChanged(StatementPeriodAvailablePropertyString); RaisePropertyChanged(NotDetailedMoviesDetailPropertyString); Movie.MovieDetailFetchedList.CollectionChanged += MoviesDetailListChanged; RaisePropertyChanged(AllMoviesDetailPropertyString); RaisePropertyChanged(DetailedMoviesDetailPropertyString); RaisePropertyChanged(NotDetailedHeaderPropertyString); } } } private void MoviesDetailListChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (var item in e.NewItems) { if (((MovieDetail) item).IsDetailed()) DetailedMoviesDetail.Add(item as MovieDetail); else NotDetailedMoviesDetail.Add(item as MovieDetail); } } else { RaisePropertyChanged(AllMoviesDetailPropertyString); RaisePropertyChanged(DetailedMoviesDetailPropertyString); RaisePropertyChanged(NotDetailedMoviesDetailPropertyString); } } #endregion private MovieDetailFetchedList _allMoviesDetail; public MovieDetailFetchedList AllMoviesDetail { get { if (Movie == null) return new MovieDetailFetchedList(); return _allMoviesDetail ?? (AllMoviesDetail = Movie.MovieDetailFetchedList); } set { if (_allMoviesDetail != value) { if (_allMoviesDetail != null) _allMoviesDetail.CollectionChanged -= MoviesDetailListChanged; _allMoviesDetail = value; _allMoviesDetail.CollectionChanged += MoviesDetailListChanged; RaisePropertyChanged(AllMoviesDetailPropertyString); //force update DetailedMoviesDetail = NotDetailedMoviesDetail = null; RaisePropertyChanged(DetailedMoviesDetailPropertyString); RaisePropertyChanged(HasDetailedMoviesDetailPropertyString); RaisePropertyChanged(NotDetailedMoviesDetailPropertyString); RaisePropertyChanged(HasNotDetailedMoviesDetailPropertyString); } } } public bool HasNotDetailedMoviesDetail { get { return NotDetailedMoviesDetail != null && NotDetailedMoviesDetail.Count > 0; } } private ObservableCollection<MovieDetail> _notDetailedMoviesDetail; public ObservableCollection<MovieDetail> NotDetailedMoviesDetail { get { if (Movie == null) return new ObservableCollection<MovieDetail>(); return AllMoviesDetail; return _notDetailedMoviesDetail ?? //make sure RaisePropertyChanged happens by using property setter (NotDetailedMoviesDetail = AllMoviesDetail.Where(mem => !mem.IsDetailed()).ToObservableCollection()); } set { _notDetailedMoviesDetail = value; RaisePropertyChanged(NotDetailedMoviesDetailPropertyString); RaisePropertyChanged(HasNotDetailedMoviesDetailPropertyString); } } public bool HasDetailedMoviesDetail { get { return DetailedMoviesDetail != null && DetailedMoviesDetail.Count > 0; } } private ObservableCollection<MovieDetail> _DetailedMoviesDetail; public ObservableCollection<MovieDetail> DetailedMoviesDetail { get { if (Movie == null) return new ObservableCollection<MovieDetail>(); return AllMoviesDetail; return _DetailedMoviesDetail ?? //make sure RaisePropertyChanged happens by using property setter (DetailedMoviesDetail = AllMoviesDetail.Where(mem => mem.IsDetailed()).ToObservableCollection()); } set { _DetailedMoviesDetail = value; RaisePropertyChanged(DetailedMoviesDetailPropertyString); RaisePropertyChanged(HasDetailedMoviesDetailPropertyString); } } private string _DetailedHeader; public string DetailedHeader { get { return _DetailedHeader ?? (_DetailedHeader = AppResources.in_available); } set { _DetailedHeader = value; } } public string NotDetailedHeader { get { return (Movie != null && Movie.Category == Category.DRAMA) ? AppResources.Movie_MoviesDetail : AppResources.not_in_available; } } } 来做到这一点
{{1}}

1 个答案:

答案 0 :(得分:4)

所有属性获取者(AllMoviesDetail除外)都有两个返回语句。由于只会执行第一个,因此未分配值且PropertyChanged事件未被触发。