Daro
Daro

Reputation: 2010

Data Bound ComboBox partially unclickable

I have some ComboBoxes set up like this:

    <ComboBox Name="CB_OS" Grid.Row="5" ItemsSource="{Binding OS_Sellection}" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem Content="{Binding Name}" IsSelected="{Binding IsSelected, Mode=TwoWay}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

The ComboBox fills with ComboBoxItems correctly, but clicking on the text (Content) does not select the Item. I must click elsewhere, where there is no text to actually change the selected item.

If I change it to this:

<ComboBox Name="CB_OS" Grid.Row="5" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
            <ComboBoxItem Content="OOOOOOOOO"/>
            <ComboBoxItem Content="OOOOOOOOO"/>
            <ComboBoxItem Content="OOOOOOOOO"/>
</ComboBox>

it works fine.

OS_Selection contains only the following members:

private string name;
private bool isChecked;
private bool isSelected;

So my question is: How can I make the entire row (item) clickable?

Upvotes: 0

Views: 80

Answers (1)

Fede
Fede

Reputation: 44038

Don't put a ComboBoxItem inside the ComboBox.ItemTemplate

Change this:

     <ComboBox Name="CB_OS" Grid.Row="5" ItemsSource="{Binding OS_Sellection}" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem Content="{Binding Name}" IsSelected="{Binding IsSelected, Mode=TwoWay}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

for this:

<ComboBox Grid.Row="5" Margin="2"
          ItemsSource="{Binding OS_Sellection}"
          DisplayMemberPath="Name"/>

Also, WPF does not bind to private fields, only public properties.

Upvotes: 1

Related Questions