Ben
Ben

Reputation: 57209

WPF ComboBoxItem DataTemplate can't click item text to select

I'm populating a ComboBox with an ItemsSource and showing a simple Binding:

<ComboBox 
    ItemsSource="{Binding Locations}" 
    SelectedItem="{Binding Location}"
>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ComboBoxItem Content="{Binding Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I can't choose a new combobox item by clicking the text (I have to click outside of the text):

enter image description here

So, if I click the darker blue area inside the inner border (the text container), it doesn't update the selection. If I click in the lighter blue area, it updates as expected. Why is this happening?

Upvotes: 0

Views: 387

Answers (1)

Clemens
Clemens

Reputation: 128062

There shouldn't be a ComboBoxItem in the ItemTemplate (because it is used as the ContentTemplate of another, automatically generated ComboBoxItem).

Use a TextBlock instead:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</ComboBox.ItemTemplate>

Or just drop the entire ItemTemplate and just set DisplayMemberPath:

<ComboBox 
    DisplayMemberPath="Name"
    ItemsSource="{Binding Locations}" 
    SelectedItem="{Binding Location}"/>

Upvotes: 2

Related Questions