2012-06-27 63 views

回答

1

我发现在另一篇文章的解决方案,我不得不调整它一点点:What is the proper way to handle multiple datagrids in a tab control so that cells leave edit mode when the tabs are changed?

基本上我已经加入我的TabControl的PreviewMouseDown产生不同的工作区的事件处理程序。

private void TabControl_PreviewMouseDown(object sender, MouseButtonEventArgs e) { 
     MainWindow_VM dc = (MainWindow_VM)this.DataContext; 

     if (IsUnderTabHeader(e.OriginalSource as DependencyObject)) 
      //Do what need to be done before switching workspace 
      // in my case, switch the focus to a dummy control so the objectContext would save everything, even the currently focused textbox 

    } 

private bool IsUnderTabHeader(DependencyObject control) 
    { 
     if (control is TabItem) 
     { 
      return true; 
     } 
     DependencyObject parent = VisualTreeHelper.GetParent(control); 
     if (parent == null)   
      return false; 

     return IsUnderTabHeader(parent); 
    } 
0

您应该能够将选项卡的“当前”项绑定到模型中的变量。当这个变化时,请做你的工作。