MahApps.Metro
MahApps.Metro copied to clipboard
Prevent MetroTabItem closing
I just got a small question cuz I don't know how to fix it.
I'm using the MetroTabItem controls because they come with a close button "out the box". The content of the tabs is text that can be edited by the user. I want to include a save prompt, when closing the tab without saving the text. But I don't know how to actually "cancel" the closing process. The CanExecute method for the CloseTabCommand is being called when clicking on the close button, but even if I return false, the tab is being closed. I don't know if this is a bug or intended behaviour but regardless, it is quite troublesome.
Anything I can do to prevent the tab from closing?
Hi @MaxDeBy
For a notmal TabControl
this is marked as todo in the source, so I think this is indeed a bug:
https://github.com/MahApps/MahApps.Metro/blob/29a0ac40a9b99e192aeff63a6def6580c0203076/src/MahApps.Metro/Actions/CloseTabItemAction.cs#L55
For MetroTabControl
there is a demo available which shows how to prevent the closing event. The demo lives here:
https://github.com/MahApps/MahApps.Metro/blob/29a0ac40a9b99e192aeff63a6def6580c0203076/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TabControlExamples.xaml#L290-L334
Happy coding Tim
Hey @timunie
Thanks for pointing me towards the example. I didn't bother to try and check the TabControl (or MetroTabControl in this case) itself. It just didn't seem logical to me to look at the parent for an event that's about a specific instance of a child.
Regardless, that did the trick. Thanks a lot.
@MaxDeBy
can you reopen the issue as I think there is a bug anyway?
Moreover here is very nice lib for handling tabs: https://dragablz.net
Happy coding Tim
Woops. Didn't see your message. I'll reopen it then.