I’m working on a ComboBox that has to have a CheckBox on its items. I’ve implemented a functionality to attend changes on those checkboxes. Part of that functionality establishes the content of the ComboBox from the CheckBoxes (i.e. when I select ‘pear’, ‘apple’ and ‘blueberry’, the combo has to have those items checked and also its content has
to be ‘pear; apple; blueberry’).
I’ve done it. But, since I’m working with a ComboBox, items are selected whenever I click on the item outside its checkbox so the content is overwritten by the text of the item. This xaml shows how is it written:
<ComboBox IsEditable="True" DataContext="{Binding}"
IsTextSearchEnabled="False" IsReadOnly="True">
<ComboBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=EstaSeleccionado,
Mode = TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding Path=Item.Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
How do I disable the listitems without disabling the checkboxes?
UPDATE
In your answer try to skip events. I know this can be done more or less clean and easy; but for maintainability concerns I would prefer a property-xaml based solution (i.e. disable events on the ListBoxItem except on the checkbox’s screen area, etc.)
The Checkbox control has a Content area to the right of the Checkbox for adding text, graphics, etc. Instead of setting your text in a separate textblock use the CheckBox’s Conent tag.
Note: the Content tag does not need to be explicitly set, it is assumed that controls between the open and closing Checkbox tags are Content.
Update:
Sorry, I misunderstood your question. The problem you’re seeing is that the combobox default behavior is on SelectionChanged is to set the SelectedText to the newly SelectedItem. To change this behavior you have two options:
I’d recommend option #2, something like this:
Code:
XAML:
Apologies, I don’t have my compiler handy so some of this might not compile.