Note that there are some explanatory texts on larger screens.

plurals
  1. POIn WPF how do I bind a combobox to TabControl Items?
    primarykey
    data
    text
    <p>I am learning WPF and I have a question about binding a ComboBox</p> <p>I have a simple WPF window with the following controls: a ComboBox at the top, and a TabControl under it.</p> <p>I populate the tabcontrol with two standard TabItems. Then I add a few more tabs to it - however these are of my own "MyTab" class that simply inherits TabItem. I'm not overriding anything or doing anything exotic, I just added a few public properties.</p> <p>I want to use the ComboBox as a navigation tool. Each tab has a header, and that same header should become an item in the ComboBox. If selected out of the ComboBox, it should make that corresponding tab the active one.</p> <p>Here is my setup on the ComboBox:</p> <pre><code> &lt;Window.Resources&gt; &lt;DataTemplate x:Key="EmployeeListTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Header}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>.. and then further down in the Xaml ...</p> <pre><code>&lt;ComboBox Grid.Row="1" Width="500" Name="qlist" ItemsSource="{Binding ElementName=tabControl1, Path=Items}" ItemTemplate="{StaticResource EmployeeListTemplate}" /&gt; </code></pre> <p>So I am just binding to the TabControl's Item collection and picking Header as the SelectedItem's property to display.</p> <p>It works, and when I click the ComboBox, the headers are indeed displayed as individual text items. But when I make an actual selection, that tab itself gets rendered in the selection area of that combobox!</p> <p>How do I fix this? Why does it use the Header property for the list, but when I pick from the list, the value it displays becomes the actual TabItem object? I feel like I'm missing something obvious</p> <p>By the way, I know I said I want to use this to control navigation, but I stripped all of that out and I will worry about it after I figure out this maddening thing.</p> <p><img src="https://i.stack.imgur.com/xO8ti.png" alt="Showing proper items in combobox"></p> <p>Look carefully - when I pick tabItem1 from the combobox, it actually re-rendered that tab item inside the control. You can see the top part of it, surrounded by the standard blue selection background a list item uses. maddening.</p> <p><img src="https://i.stack.imgur.com/cAZqs.png" alt="tabItem painting in combobox"></p> <p>What am I doing wrong here? Thanks for any help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload