title: WPF Tab Control Prevent Tab Change
body: <p>I'm trying to develop a system maintenance screen for my application in which I have several tabs each representing a different maintenance option i.e. maintain system users et cetera. Once a user clicks on edit/new to change a existing record I want to prevent navigating away from the current tab until the user either clicks save or cancel.</p>
  
  <p>After some googling I've found a link <a href="http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/">http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/</a> which seemed to solve my problem, or so I thought.</p>
  
  <p>I've tried implementing this, but my event never seems to fire. Below is my XAML.</p>
  
  <pre><code>&lt;TabControl Name="tabControl"&gt;
      &lt;TabItem Header="Users"&gt;
          &lt;DockPanel&gt;
              &lt;GroupBox Header="Existing Users" Name="groupBox1" DockPanel.Dock="Top" Height="50"&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;Label Margin="3,3,0,0"&gt;User:&lt;/Label&gt;
                      &lt;ComboBox Width="100"  Height="21" Margin="3,3,0,0"&gt;&lt;/ComboBox&gt;
                      &lt;Button Width="50" Height="21" Margin="3,3,0,0" Name="btnUsersEdit" Click="btnUsersEdit_Click"&gt;Edit&lt;/Button&gt;
                      &lt;Button Width="50" Height="21" Margin="3,3,0,0" Name="btnUsersNew" Click="btnUsersNew_Click"&gt;New&lt;/Button&gt;
                  &lt;/StackPanel&gt;
              &lt;/GroupBox&gt;
              &lt;GroupBox Header="User Information" Name="groupBox2"&gt;
                  &lt;Button Content="Cancel" Height="21" Name="btnCancel" Width="50" Click="btnCancel_Click" /&gt;
              &lt;/GroupBox&gt;
          &lt;/DockPanel&gt;
      &lt;/TabItem&gt;
      &lt;TabItem Header="User Groups"&gt;
  
      &lt;/TabItem&gt;        
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>And this is my code</p>
  
  <pre><code>public partial class SystemMaintenanceWindow : Window
  {
  
      private enum TEditMode { emEdit, emNew, emBrowse }
  
      private TEditMode _EditMode = TEditMode.emBrowse;        
  
      private TEditMode EditMode
      {
          get { return _EditMode; }
          set 
          { 
              _EditMode = value; 
          }
      }        
  
      public SystemMaintenanceWindow()
      {
          InitializeComponent();
  
          var view = CollectionViewSource.GetDefaultView(tabControl.Items.SourceCollection);
          view.CurrentChanging += this.Items_CurrentChanging;
      }        
  
      void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
      {
          if ((e.IsCancelable) &amp;&amp; (EditMode != TEditMode.emBrowse))
          {
              var item = ((ICollectionView)sender).CurrentItem;
              e.Cancel = true;
              tabControl.SelectedItem = item;
  
              MessageBox.Show("Please Save or Cancel your work first.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
          }
      }        
  
      private void btnUsersNew_Click(object sender, RoutedEventArgs e)
      {
          EditMode = TEditMode.emNew;
      }
  
      private void btnUsersEdit_Click(object sender, RoutedEventArgs e)
      {
          EditMode = TEditMode.emEdit;
      }
  
      private void btnCancel_Click(object sender, RoutedEventArgs e)
      {
          EditMode = TEditMode.emBrowse;
      }
  }
  </code></pre>
  
  <p>Apologies now if I'm being stupid, but for the life of me I cannot workout see why my event does not fire when the user clicks between tabs.</p>
  
  <p>Thanks for all your help.</p>
  
  <p>Emlyn</p>