如何在子视图模型属性更改时通知父视图模型?

3

我有一个Windows窗口,名为:

  1. 相册(父窗口)
  2. 相册属性(子窗口)

在相册窗口中,我有一个下拉列表框,用于选择相册,还有一个新建相册按钮、保存按钮和属性按钮。

我想要只有在我的相册处于编辑模式时才能启用保存按钮。当我添加新照片到一个相册中或者通过点击属性按钮更改属性时,相册将进入编辑模式。

我有以下属性:

PhotoAlbumVM 中的 IsPhotoAlbumUpdated

PhotoAlbumPropertyVM 中的 IsPhotoAlbumPropertyUpdated

IsSaveEnabled
{
   get return this.IsPhotoAlbumUpdated || this.SelectedAlbum.IsPhotoAlbumPropertyUpdated;
}

 in PhotoAlbumVM

<Button Name="BtnSave" Command="{Binding Save}"
                    ToolTip="{x:Static resx:Resource.ToolTipSave}" Focusable="True"
                    IsEnabled="{Binding IsSaveEnabled}">

现在,当this.SelectedAlbum.IsPhotoAlbumPropertyUpdated发生更改时,我的父视图模型即PhotoAlbumVM应该如何知道?
我想使用prism事件,但对于这样的小事情,我不想使用prism事件。
请为我提供替代逻辑建议。

1
事件聚合器并不是很难。请查看@Rachel的轻量、简单且实用的事件聚合器版本: https://rachel53461.wordpress.com/2011/10/09/simplifying-prisms-eventaggregator/ - StepUp
是的,我实现了那个功能。但它会导致性能问题,我们已经在应用程序的许多地方使用了它。 - Mohini Mhetre
2个回答

0
你需要监听子项的 OnPropertyChanged 事件。每次更改 SelectedAlbum,在 set 中移除旧专辑的处理程序,除非它为 null,使用 album.PropertyChanged -= MyPropertyChanged,并使用 value.PropertyChanged += MyPropertyChanged 将处理程序分配给新值。在 MyPropertyChanged 中强制更新 IsSaveEnabled 的新值。

0
您可以订阅PropertyChanged事件来监听集合中的项在CollectionChanged时的变化。只要您的UI正确地绑定到ObservableCollection中的项,当集合中的项的属性发生更改时,您不需要告诉UI进行更新。您可以对特定对象进行集合,或者使用下面的实现方式在整个应用程序中进行一致的行为。
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections;

namespace VJCollections
{
    /// <summary>
    ///     This class adds the ability to refresh the list when any property of
    ///     the objects changes in the list which implements the INotifyPropertyChanged. 
    /// </summary>
    /// <typeparam name="T">
    public class ItemsChangeObservableCollection<T> : 
           ObservableCollection<T> where T : INotifyPropertyChanged
    {
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                RegisterPropertyChanged(e.NewItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                UnRegisterPropertyChanged(e.OldItems);
            }
            else if (e.Action == NotifyCollectionChangedAction.Replace)
            {
                UnRegisterPropertyChanged(e.OldItems);
                RegisterPropertyChanged(e.NewItems);
            }

            base.OnCollectionChanged(e);
        }

        protected override void ClearItems()
        {
            UnRegisterPropertyChanged(this);
            base.ClearItems();
        }

        private void RegisterPropertyChanged(IList items)
        {
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void UnRegisterPropertyChanged(IList items)
        {
            foreach (INotifyPropertyChanged item in items)
            {
                if (item != null)
                {
                    item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
                }
            }
        }

        private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接