In case you ever need to prevent the user from selecting a tab in a WPF TabControl, here’s one way to do it…
The SelectedContent of the TabControl hasn’t changed at the time that the ItemsSource’s default collection view raises its CurrentChanging event. If you decide to lock the user into the selected tab, simply set the TabControl’s SelectedIndex back to the index of the SelectedContent. No fuss, no muss…
Pragmatic. Love it 😉
Great tip Josh. Cheers
not too bad! 😉
It’s an interesting approach, but I have to question why you’d want to do this. Unless you indicate to the user that the tab item is disabled, it would be counterintuitive for a user to click a tab item and nothing to happen.
Thanks all.
Pete – The reason behind this, for me, was to prevent the user from leaving a tab that is invalid or dirty and needs to be corrected or saved. You could show a messagebox when setting the selectedindex back to the prior value, asking “Hey user, do you wanna save or what?” 🙂
Josh
Pretty slick!
clever tip.
Really neat would be then “blinking” the tab which the user isn’t allowed to leave. That way, the user gets some feedback that the command was received by the program, but there is something in the tab left to do.
How to prevent a TabItem from being selected…
DotNetBurner – burning hot .net content…