c# - WPF MVVM - 如何检测 View 是否为 "Dirty"

标签 c# wpf mvvm

我目前需要通知我的应用程序用户,如果 View 上的任何字段已更改/更新。

例如,如果用户更改 View 上的日期字段然后尝试关闭 View ,应用程序将显示一条消息,要求用户继续并丢失更改或取消,以便他们可以单击保存按钮。

问题是:我如何检测到 View 中的任何数据字段发生了变化?

希望这是有道理的,比你提前,问候,

最佳答案

您可以采取的一种方法是利用 IChangeTrackingINotifyPropertyChanged接口(interface)。

如果您创建一个抽象基类,您的 View 模型继承自 (ViewModelBase),它实现了 IChangeTrackingINotifyPropertyChanged 接口(interface),您可以让您的 View 模型基础附加到属性更改通知(实际上表示 View 模型已被修改),这将设置 IsChanged属性设置为 true 以指示 View 模型是“脏的”。

使用这种方法,您将依靠通过数据绑定(bind)的属性更改通知来跟踪更改,并在进行任何提交后重置更改跟踪。

在您描述的情况下,您可以处理 UnloadedClosing DataContext 查看您的事件;如果 DataContext 实现了 IChangeTracking,您可以使用 IsChanged 属性来确定是否进行了任何未接受的更改。

简单示例:

/// <summary>
/// Provides a base class for objects that support property change notification 
/// and querying for changes and resetting of the changed status.
/// </summary>
public abstract class ViewModelBase : IChangeTracking, INotifyPropertyChanged
{
    //========================================================
    //  Constructors
    //========================================================
    #region ViewModelBase()
    /// <summary>
    /// Initializes a new instance of the <see cref="ViewModelBase"/> class.
    /// </summary>
    protected ViewModelBase()
    {
        this.PropertyChanged += new PropertyChangedEventHandler(OnNotifiedOfPropertyChanged);
    }
    #endregion

    //========================================================
    //  Private Methods
    //========================================================
    #region OnNotifiedOfPropertyChanged(object sender, PropertyChangedEventArgs e)
    /// <summary>
    /// Handles the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for this object.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">A <see cref="PropertyChangedEventArgs"/> that contains the event data.</param>
    private void OnNotifiedOfPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e != null && !String.Equals(e.PropertyName, "IsChanged", StringComparison.Ordinal))
        {
            this.IsChanged = true;
        }
    }
    #endregion

    //========================================================
    //  IChangeTracking Implementation
    //========================================================
    #region IsChanged
    /// <summary>
    /// Gets the object's changed status.
    /// </summary>
    /// <value>
    /// <see langword="true"/> if the object’s content has changed since the last call to <see cref="AcceptChanges()"/>; otherwise, <see langword="false"/>. 
    /// The initial value is <see langword="false"/>.
    /// </value>
    public bool IsChanged
    {
        get
        {
            lock (_notifyingObjectIsChangedSyncRoot)
            {
                return _notifyingObjectIsChanged;
            }
        }

        protected set
        {
            lock (_notifyingObjectIsChangedSyncRoot)
            {
                if (!Boolean.Equals(_notifyingObjectIsChanged, value))
                {
                    _notifyingObjectIsChanged = value;

                    this.OnPropertyChanged("IsChanged");
                }
            }
        }
    }
    private bool _notifyingObjectIsChanged;
    private readonly object _notifyingObjectIsChangedSyncRoot = new Object();
    #endregion

    #region AcceptChanges()
    /// <summary>
    /// Resets the object’s state to unchanged by accepting the modifications.
    /// </summary>
    public void AcceptChanges()
    {
        this.IsChanged = false;
    }
    #endregion

    //========================================================
    //  INotifyPropertyChanged Implementation
    //========================================================
    #region PropertyChanged
    /// <summary>
    /// Occurs when a property value changes.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region OnPropertyChanged(PropertyChangedEventArgs e)
    /// <summary>
    /// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event.
    /// </summary>
    /// <param name="e">A <see cref="PropertyChangedEventArgs"/> that provides data for the event.</param>
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, e);
        }
    }
    #endregion

    #region OnPropertyChanged(string propertyName)
    /// <summary>
    /// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for the specified <paramref name="propertyName"/>.
    /// </summary>
    /// <param name="propertyName">The <see cref="MemberInfo.Name"/> of the property whose value has changed.</param>
    protected void OnPropertyChanged(string propertyName)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    #region OnPropertyChanged(params string[] propertyNames)
    /// <summary>
    /// Raises the <see cref="INotifyPropertyChanged.PropertyChanged"/> event for the specified <paramref name="propertyNames"/>.
    /// </summary>
    /// <param name="propertyNames">An <see cref="Array"/> of <see cref="String"/> objects that contains the names of the properties whose values have changed.</param>
    /// <exception cref="ArgumentNullException">The <paramref name="propertyNames"/> is a <see langword="null"/> reference (Nothing in Visual Basic).</exception>
    protected void OnPropertyChanged(params string[] propertyNames)
    {
        if (propertyNames == null)
        {
            throw new ArgumentNullException("propertyNames");
        }

        foreach (var propertyName in propertyNames)
        {
            this.OnPropertyChanged(propertyName);
        }
    }
    #endregion
}

关于c# - WPF MVVM - 如何检测 View 是否为 "Dirty",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5890212/

相关文章:

c# - WPF:如何将操纵杆实现为替代输入设备 (C#)

wpf - 重置 TranslateZoomRotateBehavior? (WPF/混合行为)

c# - DataGrid 按钮,树上的命令绑定(bind)相对源不起作用

c# - 如何更改 WPF ComboBox SelectedText 背景颜色?

c# - 有没有办法查看 .NET Core 单元测试中生成的日志消息?

c# - 你能向客户端公开 Web 服务的进度吗 - WCF

c# - 无法从线程启动 winForm

c# - 带有参数的Unity自动工厂

c# - mvvmlight Windows 8 Metro异步加载ViewModel构造函数中的数据

c# - Silverlight 4 中组合框的转换器