2012-06-12 20 views
2

的時候,我有這樣的對話框顯示:允許取消當用戶按下在主窗口中的「X」,按「X」鍵

-- Save Changes -------------------------- 
|           | 
| Do you want to save changes?   | 
|           | 
|           | 
| |Don't Save|  |Cance| |Save| | 
|__________________________________________| 

我有實施的取消按鈕的麻煩。我不知道如何告訴主窗口在用戶點擊取消後不關閉。我通過窗口的「關閉」命令/方法提示用戶使用此對話框。

回答

10

在關閉事件,你需要到事件屬性Cancel設置爲true。

void DataWindow_Closing(object sender, CancelEventArgs e) 
    { 
     MessageBox.Show("Closing called"); 

     // If data is dirty, notify user and ask for a response 
     if (this.isDataDirty) 
     { 
      string msg = "Data is dirty. Close without saving?"; 
      MessageBoxResult result = 
       MessageBox.Show(
       msg, 
       "Data App", 
       MessageBoxButton.YesNo, 
       MessageBoxImage.Warning); 
      if (result == MessageBoxResult.No) 
      { 
       // If user doesn't want to close, cancel closure 
       e.Cancel = true; 
      } 
     } 
    } 
5

時調用的處理方法具有代表事件參數(CancelEventArgs類型的參數,讓我們稱之爲e,設置e.Cancel = true的處理程序中,以防止默認行爲。

+0

[CancelEventArgs](http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.aspx)不具有已處理' 'property。 – saluce

+0

@saluce:已編輯:) – Tudor

+0

爲什麼downvote? – Tudor

相關問題