Josh Smith'的文章关于MVVM, WPF, c#:如何关闭特定的选项卡

本文关键字:何关闭 WPF 选项 MVVM Smith 文章 Josh | 更新日期: 2023-09-27 18:02:02

我一直在自己的应用程序中改编Josh Smith关于c#/WPF中的MVVM的例子,到目前为止,对于c# -WPF初学者来说相对成功。但现在又有一个障碍即将到来:

如果用户在特定选项卡上更改了任何内容,并且用户在单击选项卡关闭按钮之后,应该显示一个消息框并通知/询问用户:"更改了选项卡内容"Example-Tab",但没有保存它们。提示:如果关闭,任何内容更改将丢失!你真的要关闭这个标签页吗?"如果他然后点击消息框的ok按钮,选项卡应该被禁用/删除,并且不应该触发保存命令或类似的东西。如果他选择单击cancel按钮,则消息框应该只关闭,并且应该显示origin选项卡。

1) messagebox-interactionlogic不是巫术

2)但是我怎样才能实现关闭一个特定的标签呢?当关闭选项卡时,适当的视图/ViewModel在哪里注册或存放?我有在我所有不同的TabViewModels'一个布尔静态方法iscontenttoftextboxeschanged,返回如果对一个或多个文本框进行了任何内容更改,则为True;如果没有对任何文本框的内容进行任何内容更改,则为false。

我不知道如何关闭一个特定的选项卡!:/

Josh Smith'的文章关于MVVM, WPF, c#:如何关闭特定的选项卡

好了,我有办法了。

改变:

void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null && e.NewItems.Count != 0)
        foreach (WorkspaceViewModel workspace in e.NewItems)
        {
            if (tab.GetType() == typeof(Tab01ViewModel))
                workspace.RequestClose += this.OnWorkspaceRequestCloseTab01;
            if (tab.GetType() == typeof(Tab02ViewModel))
                workspace.RequestClose += this.OnWorkspaceRequestCloseTab02;
            if (tab.GetType() == typeof(Tab03ViewModel))
                workspace.RequestClose += this.OnWorkspaceRequestCloseTab03;
            // and so on ...
        }

    if (e.OldItems != null && e.OldItems.Count != 0)
        foreach (WorkspaceViewModel workspace in e.OldItems)
        {
            if (tab.GetType() == typeof(Tab01ViewModel))
                workspace.RequestClose -= this.OnWorkspaceRequestCloseTab01;
            if (tab.GetType() == typeof(Tab02ViewModel))
                workspace.RequestClose -= this.OnWorkspaceRequestCloseTab02;
            if (tab.GetType() == typeof(Tab03ViewModel))
                workspace.RequestClose -= this.OnWorkspaceRequestCloseTab03;
            // and so on ...
        }
}

/// <summary>
/// Closes and removes a tab of type Tab01ViewModel
/// </summary>
void OnWorkspaceRequestCloseTab01(object sender, EventArgs e)
{
    WorkspaceViewModel workspace = sender as WorkspaceViewModel;
     if(Tab01ViewModel.IsContentOfTextboxesChanged) 
    {
        var result = System.Windows.Forms.MessageBox.Show(
            "Changes to the tab-content »" + Tab01ViewModel.TabTitle +
            "« were made, without setting them." + 
            "'n'nHint: If closing, any content changes will be lost!" + 
            "'n'nDo you really want to close this tab?",
            "MessageboxTitle", MessageBoxButtons.OKCancel, 
            MessageBoxIcon.Question);
        if (result == DialogResult.OK)
        {
            workspace.Dispose();
            this.Workspaces.Remove(workspace);
        }
    }
    else 
    {
        workspace.Dispose();
        this.Workspaces.Remove(workspace);
    }
}

/// <summary>
/// Closes and removes a tab of type Tab02ViewModel
/// </summary>
void OnWorkspaceRequestCloseTab02(object sender, EventArgs e)
{
    WorkspaceViewModel workspace = sender as WorkspaceViewModel;
     if(Tab02ViewModel.IsContentOfTextboxesChanged) 
    {
        var result = System.Windows.Forms.MessageBox.Show(
            "Changes to the tab-content »" + Tab02ViewModel.TabTitle +
            "« were made, without setting them." + 
            "'n'nHint: If closing, any content changes will be lost!" + 
            "'n'nDo you really want to close this tab?",
            "MessageboxTitle", MessageBoxButtons.OKCancel, 
            MessageBoxIcon.Question);

        if (result == DialogResult.OK)
        {
            workspace.Dispose();
            this.Workspaces.Remove(workspace);
        }
    }
    else 
    {
        workspace.Dispose();
        this.Workspaces.Remove(workspace);
    }
}

/// <summary>
/// Closes and removes a tab of type Tab03ViewModel
/// </summary>
void OnWorkspaceRequestCloseTab03(object sender, EventArgs e)
{
    WorkspaceViewModel workspace = sender as WorkspaceViewModel;
     if(Tab03ViewModel.IsContentOfTextboxesChanged) 
    {
        var result = System.Windows.Forms.MessageBox.Show(
            "Changes to the tab-content »" + Tab03ViewModel.TabTitle +
            "« were made, without setting them." + 
            "'n'nHint: If closing, any content changes will be lost!" + 
            "'n'nDo you really want to close this tab?",
            "MessageboxTitle", MessageBoxButtons.OKCancel, 
            MessageBoxIcon.Question);

        if (result == DialogResult.OK)
        {
            workspace.Dispose();
            this.Workspaces.Remove(workspace);
        }
    }
    else 
    {
        workspace.Dispose();
        this.Workspaces.Remove(workspace);
    }
}