2012-12-10 99 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(); 
     } 
    }