2014-05-05 54 views
0

在我自己的應用程序中,我一直在調整Josh Smith關於MVVM在C#/ WPF中的示例,直到現在對於C#-WPF初學者來說相對成功。但現在又有一個障礙即將到來:Josh Smith關於MVVM,WPF,C#的文章:如何關閉特定選項卡?

如果用戶在特定選項卡上更改了任何內容,並且用戶在該選項卡關閉按鈕之後單擊,則應顯示消息框並通知/詢問用戶:「對標籤內容進行了更改」Example-Tab「,但沒有保存它們。提示:如果關閉,任何內容更改都將丟失!您是否真的要關閉此標籤?如果他然後點擊消息框的OK按鈕,該選項卡應該被禁用/刪除,並且不應該激發保存命令或類似的東西。如果他或者點擊「取消」按鈕,只應關閉消息框並顯示原始標籤。

1)messagebox-interactionlogic不是巫術。

2)但我怎麼能意識到關閉一個特定的選項卡?關閉選項卡時,註冊或存放的專用View/ViewModel在哪裏?我在所有不同的TabViewModel中都有布爾靜態方法IsContentOfTextboxesChanged,如果對一個或多個文本框進行了任何內容更改,則返回 ;如果沒有對任何文本框內容進行內容更改,則返回false。

我不幸不知道如何關閉特定標籤! :/

回答

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

你能提供更多的代碼是什麼標籤?它是如何實例化的? – CharlesMighty