MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.27k stars 2.45k forks source link

Prevent MetroTabItem closing #4078

Open MaxDeBy opened 3 years ago

MaxDeBy commented 3 years ago

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?

timunie commented 3 years ago

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

MaxDeBy commented 3 years ago

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.

timunie commented 3 years ago

@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

MaxDeBy commented 3 years ago

Woops. Didn't see your message. I'll reopen it then.