Josh Smith关于MVVM,WPF,C#的文章:如何关闭特定标签?

时间:2014-05-05 19:16:41

标签: c# wpf mvvm tabs

在我自己的应用程序中,我一直在调整Josh Smith关于C#/ WPF中MVVM的例子,到目前为止,C#-WPF-beginner相对成功。但现在还有一个障碍即将来临:

如果用户在特定选项卡上更改了任何内容,并且用户点击了选项卡关闭按钮,则应显示消息框并通知/询问用户:“对选项卡内容的更改”示例-Tab“是在没有保存的情况下制作的。提示:如果关闭,任何内容更改都将丢失!你真的想要关闭这个标签吗?”如果他然后单击消息框的OK按钮,则应禁用/删除该选项卡,并且不应触发save-commands或类似的东西。如果他或者单击CANCEL按钮,则只应关闭消息框并显示原始选项卡。

1)messagebox-interactionlogic不是巫术。

2)但是如何才能实现关闭特定标签?关闭标签时注册或存放的适当View / ViewModel在哪里?我在我所有不同的TabViewModels中都有一个布尔静态方法IsContentOfTextboxesChanged,它返回 如果对一个或多个文本框进行了任何内容更改,则为true;如果未对任何文本框内容进行任何内容更改,则为false。

我不知道如何关闭特定标签! :/

1 个答案:

答案 0 :(得分:1)

好的,我已经找到了解决方案。

更改:

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);
    }
}