2012-12-10 26 views
2

我有一個文本框綁定到viewModel中的屬性。我已經在viewmodel中進行了驗證檢查,並檢查用戶是否對數據進行了任何更改。所以在退出時要求用戶將更改提交到數據庫。文本框丟失焦點沒有觸發對話框從頂部關閉

問題我面臨的是當我更改文本框中的值,用戶直接單擊關閉按鈕對話框時,失去的焦點不會發生,並且屬性中的值不會更改。所以我用

UpdateSourceTrigger=PropertyChanged 

我確實改變了屬性,但爲每個按鍵創建一個撤銷堆棧的條目。我想僅在失去焦點時更新屬性,即使用戶在對話框頂部關閉按鈕時也可以更改一個變更集。

回答

1

請點擊此鏈接,它是針對此問題有幫助:UpdateSourceTrigger LostFocus OnClosing Problem

+1

嗨,歡迎StackOverflow上,發佈其有用引述該頁面的初步認識到您的實際的答案(一個鏈接的情況下,當鏈接在某個點死亡)。 –

0

也許會要求用戶保存,如果他有變化,他試圖關閉?

 public MainWindow() 
     { 
      InitializeComponent(); 

      this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing); 
     } 

     void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
     { 
      //ask the user to save , if needed to 
     } 
0

您可以將以下內容添加到您的app.xaml.cs.然後你的UpdateSourceTrigger = LostFocus應該可以工作。

protected override void OnStartup(StartupEventArgs e) 
    { 
     EventManager.RegisterClassHandler(typeof(Button), ButtonBase.ClickEvent, new RoutedEventHandler(ButtonClick)); 
     //... 
    } 

    private void ButtonClick(object sender, RoutedEventArgs e) 
    { 
     if (sender != null && sender is Button) 
     { 
      (sender as Button).Focus(); 
     } 
    }