我有一个应用程序使用Josh Smith的MVVM模式(http://msdn.microsoft.com/en-us/magazine/dd419663.aspx)构建。MVVM中捕获“切换工作区”事件
当我在应用程序中打开几个工作区时,我想捕获切换工作区/选项卡的事件,以便我可以先保存当前工作区的内容。我已经查看了WorkspaceViewModel和ViewModelBase,但我不知道如何添加该EventHandler。
我有一个应用程序使用Josh Smith的MVVM模式(http://msdn.microsoft.com/en-us/magazine/dd419663.aspx)构建。MVVM中捕获“切换工作区”事件
当我在应用程序中打开几个工作区时,我想捕获切换工作区/选项卡的事件,以便我可以先保存当前工作区的内容。我已经查看了WorkspaceViewModel和ViewModelBase,但我不知道如何添加该EventHandler。
我发现在另一篇文章的解决方案,我不得不调整它一点点: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);
}
您应该能够将选项卡的“当前”项绑定到模型中的变量。当这个变化时,请做你的工作。