Note that there are some explanatory texts on larger screens.

plurals
  1. POComboBox with Checkboxes closes on Checkbox Click
    primarykey
    data
    text
    <p>I have <a href="https://stackoverflow.com/questions/6503022/wpf-combobox-with-checkbox-selecteditem">this</a> same problem, but the solution presented isn't working, nor is any other I've found. I want to create a <code>ComboBox</code> with <code>CheckBox</code>es as part of the <code>ItemTemplate</code>. This has been accomplished. But the problem arises when the user clicks a <code>CheckBox</code>: the <code>PopUp</code> closes. I need it to stay open.</p> <p>I tried handling the <code>ComboBox.SelectionChanged</code> event and the <code>CheckBox.Click</code> 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 <code>CheckBox</code>, which is matches the behavior of the control as nothing appears in the <code>TextBox</code> portion.</p> <p>This is not for multiple selection, but rather to have the <code>CheckBox</code> bind to a property in the data context.</p> <p>Here is some sample code</p> <pre><code>&lt;Toolbar VerticalAlignment="Top"&gt; &lt;ComboBox x:Name="comboBox" SelectionChanged="ComboBox_SelectionChanged"&gt; &lt;ComboBox.ItemTemplate&gt; &lt;DataTemplate DataType="local:MyType"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;CheckBox Click="CheckBox_Clicked"/&gt; &lt;TextBlock Text="{Binding Title}" Grid.Column="1"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ComboBox.ItemTemplate&gt; &lt;local:MyType Title="item 1"/&gt; &lt;local:MyType Title="item 2"/&gt; &lt;local:MyType Title="item 3"/&gt; &lt;local:MyType Title="item 4"/&gt; &lt;/ComboBox&gt; &lt;/Toolbar&gt; 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; } </code></pre> <p>Can anyone help?</p> <p><strong>EDIT:</strong></p> <p>I noticed that there is a difference of behavior when the <code>ComboBox</code> is placed in a <code>Toolbar</code>. When not in the <code>Toolbar</code>, it behaves as expected: the <code>CheckBox</code> changes state without closing the <code>Popup</code>. But in the <code>ToolBar</code>, the <code>Popup</code> closes on the first click, regardless of where the click is. Try the new code, please. I really need this in a toolbar.</p> <p><strong>EDIT 2:</strong></p> <p>For posterity and anyone searching for it, <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/859bae90-2f92-42dc-af79-46b9193f0dd2" rel="nofollow noreferrer">MS suggested</a> setting the Focusable property of the CheckBox in the DataTemplate to false. This achieves the desired effect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload