I have this same problem, but the solution presented isn't working, nor is any other I've found. I want to create a ComboBox
with CheckBox
es as part of the ItemTemplate
. This has been accomplished. But the problem arises when the user clicks a CheckBox
: the PopUp
closes. I need it to stay open.
I tried handling the ComboBox.SelectionChanged
event and the CheckBox.Click
event, but I can't get it. From tracing through the code, it appears that the SelectionChanged event doesn't fire at all when the user clicks the CheckBox
, which is matches the behavior of the control as nothing appears in the TextBox
portion.
This is not for multiple selection, but rather to have the CheckBox
bind to a property in the data context.
Here is some sample code
<Toolbar VerticalAlignment="Top">
<ComboBox x:Name="comboBox" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate DataType="local:MyType">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox Click="CheckBox_Clicked"/>
<TextBlock Text="{Binding Title}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
<local:MyType Title="item 1"/>
<local:MyType Title="item 2"/>
<local:MyType Title="item 3"/>
<local:MyType Title="item 4"/>
</ComboBox>
</Toolbar>
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// do some stuff
}
private void CheckBox_Clicked(object sender, RoutedEventArgs e)
{
// change a property on the data context if not data bound
// Tried this, but Popup just closes then reopens
comboBox.IsDropDownOpen = true;
// This seems to have no effect
e.Handled = true;
}
Can anyone help?
EDIT:
I noticed that there is a difference of behavior when the ComboBox
is placed in a Toolbar
. When not in the Toolbar
, it behaves as expected: the CheckBox
changes state without closing the Popup
. But in the ToolBar
, the Popup
closes on the first click, regardless of where the click is. Try the new code, please. I really need this in a toolbar.
EDIT 2:
For posterity and anyone searching for it, MS suggested setting the Focusable property of the CheckBox in the DataTemplate to false. This achieves the desired effect.