2010-07-08 17 views
1

對於應用程序具有保存狀態,最簡單的方法是什麼?這樣,無論何時更新屬性或內容,「保存」選項都會啓用。WPF:如何實現保存按鈕啓用

例如,有一個菜單和工具欄的「保存」按鈕。當WPF應用第一次打開時,兩個按鈕都被禁用。當用戶更新屬性或文檔時,這些按鈕將變爲啓用狀態,直到完成「保存」爲止,此時它們將恢復爲禁用狀態。

回答

1

IsEnabled綁定到暴露了「IsDirty」或「HasBeenModified」布爾屬性或類似行爲的ViewModel。 ViewModel會監視模型的變化,如果模型因任何原因被修改,則將IsDirty設置爲true。保存後,可以通知ViewModel將IsDirty設置爲false,禁用該按鈕。

您正在使用Model-View-ViewModel模式,對吧?下面是模式的幾個環節,以幫助你的方式,以防萬一:

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

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

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

0

都是你希望在同一個觀看的特性類?如果是這樣,這樣的事情將起作用:

  1. 有類從INotifyPropertyChanged派生;
  2. 觀看用於從類屬性的變化,並設置當有基於所述IsDirty標誌
  3. 當執行的保存命令,設置IsDirty =假
變化
  • 集的IsEnabled用於保存按鈕的IsDirty標誌

    通知類示例

    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 
    } 
    

    留意屬性更改

    public partial class MainWindow : Window 
    { 
        private bool isDirty; 
    
        NotifyingClass MyProperties = new NotifyingClass(); 
    
        public MainWindow() 
        { 
         InitializeComponent(); 
    
         this.MyProperties.PropertyChanged += (s, e) => 
          { 
           this.isDirty = true; 
          }; 
        } 
    } 
    

    如何設置禁用/啓用狀態取決於你正在做的按鈕/命令執行的是什麼類型。如果你想進一步的幫助,讓我知道你是如何做的(事件處理程序,RoutedCommand,RelayCommand,其他),我會檢查出來。