7heViking
7heViking

Reputation: 7577

Combobox selections

I have this combobox which contains a data tempate with a checkbox and a textbox. All is working but I would like to make it a little bit easier to select a value. Right now I have to click on the chechbox to change the value of the check box. Now I would like to be able to just click on the item in the combobox which also should toggle the checkbox.

Is this possible? If yes then how?

Here is a picture of my solution right now

enter image description here

Here is the code for my combobox

<ComboBox Name="employeeComboBox" Margin="2,0,2,0"
    ScrollViewer.CanContentScroll="False"
    DataContext="{Binding EmployeesOverviewViewModel, Source={StaticResource ViewModelLocator}}"
    ItemsSource="{Binding Employees}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding Path=IsSelected}" Margin="2,0,2,2" VerticalAlignment="Center"/>
                <TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 1

Views: 425

Answers (3)

K Mehta
K Mehta

Reputation: 10533

Why don't you use the Content property on the CheckBox?

<CheckBox Content="Hello, World" />

This way, the checkbox would toggle even when you click on the text (content).

As for your specific case, you can bind Name to Content instead of creating a separate TextBlock for it, and it should work as you want it to.

Upvotes: 2

Fischermaen
Fischermaen

Reputation: 12458

Change your DataTemplate to this:

<DataTemplate>
    <CheckBox IsChecked="{Binding Path=IsSelected}" 
              Margin="2,0,2,2" 
              Content="{Binding Path=Name}" 
              VerticalAlignment="Center"/>
</DataTemplate> 

and it should work.

Upvotes: 3

Muhammad Irfan
Muhammad Irfan

Reputation: 131

Whats happning here is, Your Checkbox and text are two different entities, you need to make them one, by simply using checkbox's text property and binding it. that way whenever you click on text your checkbox gets selected.

Upvotes: 1

Related Questions