title: WPF: Bind TabControl SelectedIndex to a View Model's Enum Property
body: <p>I have a property on my ViewModel that is an enum:</p>
  
  <p>ViewModel:</p>
  
  <pre><code>public MyViewModel {
  
      // Assume this is a DependancyProperty
      public AvailableTabs SelectedTab { get; set; } 
  
      // Other bound properties
      public string Property1 { get; set; }
      public string Property2 { get; set; }
      public string Property3 { get; set; }
  }
  
  public enum AvailableTabs {
      Tab1,
      Tab2,
      Tab3
  }
  </code></pre>
  
  <p>I'd like to be able to bind SelectedIndex (or SelectedItem) of my TabControl to this property and have it correctly set the appropriate tab using a converter. Unfortunately, I'm a bit stuck. I know I can easily just use the SelectedIndex in my model, but I want the flexibility of re-ordering the tabs without breaking anything. I've given each TabItem a Tag property of the applicable enum value.</p>
  
  <p>My XAML:</p>
  
  <pre><code>&lt;TabControl Name="MyTabControl" SelectedIndex="{Binding SelectedTab, Converter={StaticResource SomeConverter}}"&gt;
      &lt;TabItem Header="Tab 1" Tag="{x:Static local:AvailableTabs.Tab1}"&gt;
          &lt;TextBlock Text="{Binding Property1}" /&gt;
      &lt;/TabItem&gt;
      &lt;TabItem Header="Tab 2" Tag="{x:Static local:AvailableTabs.Tab2}"&gt;
          &lt;TextBlock Text="{Binding Property2}" /&gt;
      &lt;/TabItem&gt;
      &lt;TabItem Header="Tab 3" Tag="{x:Static local:AvailableTabs.Tab3}"&gt;
          &lt;TextBlock Text="{Binding Property3}" /&gt;
      &lt;/TabItem&gt;
  &lt;/TabControl&gt;
  </code></pre>
  
  <p>My problem is that I can't figure out how to get the TabControl into my converter so I can do:</p>
  
  <pre><code>// Set the SelectedIndex via the enum (Convert)
  var selectedIndex = MyTabControl.Items.IndexOf(MyTabControl.Items.OfType&lt;TabItem&gt;().Single(t =&gt; (AvailableTabs) t.Tag == enumValue));
  
  // Get the enum from the SelectedIndex (ConvertBack)
  var enumValue = (AvailableTabs)((TabItem)MyTabControl.Items[selectedIndex]).Tag;
  </code></pre>
  
  <p>I'm afraid I might be overthinking it. I tried using a MultiValue converter without much luck. Any ideas?</p>