Reputation: 67
I've been adapting Josh Smith's example about MVVM in C#/WPF in my own application up to now relatively successful for a C#-WPF-beginner. But now there is one more barrier upcoming:
If any content changes werde made by the user on a specific tab and the user clicks after that the tab-close-button, a messagebox should be shown and informs/ asks the user: "Changes to the tab-content "Example-Tab" were made, without saving them. Hint: If closing, any content changes will be lost! Do you really want to close this tab?" If he then clicks the OK-button of the messagebox, the tab should be disabled/ removed and no saving-commands or something like this should fire. If he alternatively clicks the CANCEL-button the messagebox should only be closed and the origin tab should be displayed.
1) The messagebox-interactionlogic is no sorcery.
2) But how can I realise to close a specific tab? Where are the appropriated View/ ViewModel registered or deposited when closing a tab? I have in all my different TabViewModels' a boolean static method IsContentOfTextboxesChanged, that returns true, if any content changes were made to one or more textboxes, or false, if no content changes were made to any textbox-contents.
I have unfortuantely no idea how to close a specific tab! :/
Upvotes: 0
Views: 291
Reputation: 67
Ok, I've got the solution.
Change:
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);
}
}
Upvotes: 1