How to prevent a TabItem from being selected

In case you ever need to prevent the user from selecting a tab in a WPF TabControl, here’s one way to do it…

ControllingTheTabControl

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…

9 Responses to How to prevent a TabItem from being selected

  1. Laurent says:

    Pragmatic. Love it 😉

  2. Great tip Josh. Cheers

  3. Fabrizio says:

    not too bad! 😉

  4. peteohanlon says:

    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.

  5. Josh Smith says:

    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

  6. kaynan says:

    clever tip.

  7. Daniel Rose says:

    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.

  8. How to prevent a TabItem from being selected…

    DotNetBurner – burning hot .net content…