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…



September 4, 2009 at 11:16 pm
Pragmatic. Love it
September 5, 2009 at 2:20 am
Great tip Josh. Cheers
September 5, 2009 at 6:40 am
not too bad!
September 5, 2009 at 7:04 am
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.
September 5, 2009 at 8:01 am
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
September 5, 2009 at 4:02 pm
Pretty slick!
September 6, 2009 at 6:21 am
clever tip.
September 7, 2009 at 4:34 am
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.
September 12, 2009 at 3:57 pm
How to prevent a TabItem from being selected…
DotNetBurner – burning hot .net content…