Daniel Jørgensen
Daniel Jørgensen

Reputation: 1202

WPF - Combobox does not show selecteditem

Im new to WPF and VB and im having some trouble figuring out why the thing i select in a ComboBox dropdown does not show in the ComboBox after selection.

I have my ComboBox populated through bindings and DataContext. This is my Settings.xaml file

<Window.Resources>
    <DataTemplate x:Key="TabList">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Header}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>
...
<ComboBox x:Name="tabs" 
          ItemsSource="{Binding tabList}"                                       
          ItemTemplate="{StaticResource ResourceKey=TabList}" 
          Height="32" />

The views codebehind file (Settings.xaml.vb) then loads the ComboBoxes content in the class' constructor, and the data does show in the ComboBox

Public Class Settings

    Private loader As SettingsLoader

    Sub New()
        InitializeComponent()
        Dim sh As New SettingsHandler(True)
        loader = New SettingsLoader
        loader.tabList = sh.Current.Tabs
        DataContext = loader
    End Sub
End Class

The SettingsLoader class looks like so. TRTab is my own class that simply inherits from TabItem and only adds a few extra properties, nothing fancy

Public Class SettingsLoader

    Private _tabs As List(Of TRTab)

    Public Property tabList() As List(Of TRTab)
        Get
            Return _tabs
        End Get
        Set(value As List(Of TRTab))
            _tabs = value
        End Set
    End Property

End Class

Do i need to add a property to my SettingsLoader that holds the selected item for the ComboBox to show or what am i missing ?

EDIT: Just to clarify what im trying to achieve: I have a TabControl with a number of tabs. Those tabs' Headers needs to be also shown in a ComboBox for selection

Upvotes: 0

Views: 512

Answers (1)

mm8
mm8

Reputation: 169200

Because TabItem is a ContentControl the ComboBox will display its Content when the item is selected. You could confirm this yourself using the following XAML markup:

<Window.Resources>
    <DataTemplate x:Key="TabList">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Header}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ComboBox x:Name="tabs" Height="32"
                  ItemTemplate="{StaticResource TabList}">
        <TabItem Content="Content" Header="Header" />
    </ComboBox>
</StackPanel>

When you open the dropdown, you will see "Header" but when you select the item and close the dropdown you will see "Content".

It generally doesn't make a whole much sense to set the ItemsSource property of a ComboBox to an IEnumerable of ContentControls. You could bind the ItemsSource to an IEnumerable(Of String) instead. Just add another property to your SettingsLoader class:

Public Class SettingsLoader
    Private _tabs As List(Of TRTab)

    Public Property tabList() As List(Of TRTab)
        Get
            Return _tabs
        End Get
        Set(value As List(Of TRTab))
            _tabs = value
        End Set
    End Property

    Public ReadOnly Property tabHeaders() As IEnumerable(Of String)
        Get
            If _tabs Is Nothing Then
                Return Nothing
            End If
            Return _tabs.Select(Function(x) x.Header.ToString())
        End Get
    End Property
End Class

<Window.Resources>
    <DataTemplate x:Key="TabList">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ComboBox x:Name="tabs" 
          ItemsSource="{Binding tabHeaders}"                                       
          ItemTemplate="{StaticResource ResourceKey=TabList}" 
          Height="32" />
</StackPanel>

The other option is to set the Content properties of your TRTab objects to the same values ass their Header properties.

Upvotes: 2

Related Questions