简体   繁体   English

WPF:如何实现“保存”按钮启用

[英]WPF : How do I implement Save button enabling

What would be the cleanest way to have a Save state for an application so that whenever a property or content was updated the "Save" options would become enabled. 什么是清除应用程序保存状态的最干净的方法,以便每当更新属性或内容时,都将启用“保存”选项。

For example, there is a menu and toolbar "Save" buttons. 例如,有一个菜单和工具栏的“保存”按钮。 When the WPF app first opens both buttons are disabled. WPF应用程序首次打开时,两个按钮均被禁用。 When the user updates the properties or document, the buttons become enabled until a "Save" is done, at which point they go back to disabled. 当用户更新属性或文档时,按钮将变为启用状态,直到完成“保存”为止,此时它们将返回禁用状态。

Bind IsEnabled to a ViewModel that exposes an "IsDirty" or "HasBeenModified" boolean property, or something of similar ilk. IsEnabled绑定到公开“ IsDirty”或“ HasBeenModified”布尔属性或类似类似内容的ViewModel。 The ViewModel would watch for changes to the Model and set IsDirty to true if the Model is modified for any reason. 如果出于任何原因修改了模型,则ViewModel将监视模型的更改并将IsDirty设置为true。 When saved, the ViewModel can be told to set IsDirty to false, disabling the button. 保存后,可以告知ViewModel将IsDirty设置为false,从而禁用按钮。

You are using the Model-View-ViewModel pattern, right? 您正在使用Model-View-ViewModel模式,对吗? Here are a few links on the pattern to help you on your way, just in case: 以下是该模式的一些链接,以防万一:

http://en.wikipedia.org/wiki/Model_View_ViewModel http://en.wikipedia.org/wiki/Model_View_ViewModel

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://msdn.microsoft.com/zh-CN/magazine/dd419663.aspx

http://www.wintellect.com/CS/blogs/jlikness/archive/2010/04/14/model-view-viewmodel-mvvm-explained.aspx http://www.wintellect.com/CS/blogs/jlikness/archive/2010/04/14/model-view-viewmodel-mvvm-explained.aspx

Are all the properties you wish to watch for in the same class? 您是否希望在同一个班级中注意所有属性? If so, something like this will work: 如果是这样,类似这样的方法将起作用:

  1. Have the class derive from INotifyPropertyChanged; 使类从INotifyPropertyChanged派生;
  2. Watch for property changes from the class and set an IsDirty flag when there are changes 监视类中的属性更改,并在发生更改时设置IsDirty标志
  3. Set IsEnabled for the Save button based on the IsDirty flag 根据IsDirty标志为“保存”按钮设置IsEnabled
  4. When the Save command is executed, set IsDirty=false 当执行保存命令时,设置IsDirty = false

Notifying Class Example 通知类示例

public class NotifyingClass : INotifyPropertyChanged
{
        private string Property1Field;
        public string Property1
        {
            get { return this.Property1Field; }
            set { this.Property1Field = value; OnPropertyChanged("Property1"); }
        }

        private string Property2Field;
        public string Property2
        {
            get { return this.Property2Field; }
            set { this.Property2Field = value; OnPropertyChanged("Property2"); }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
}

Watching for Property Changes 留意物业变化

public partial class MainWindow : Window
{
    private bool isDirty;

    NotifyingClass MyProperties = new NotifyingClass();

    public MainWindow()
    {
        InitializeComponent();

        this.MyProperties.PropertyChanged += (s, e) =>
            {
                this.isDirty = true;
            };
    }
}

How you set the disabled/enabled state depends on what type of button/command implementation you are doing. 如何设置禁用/启用状态取决于您正在执行的按钮/命令实现类型。 If you would like further help just let me know how you are doing it (event handler, RoutedCommand, RelayCommand, other) and I'll check it out. 如果您想要进一步的帮助,请告诉我您的工作方式(事件处理程序,RoutedCommand,RelayCommand等),然后我将进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM