问题
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.
回答1:
It seems that the Toolbar
control affects the ComboBox
control in some way. And strangely, the ComboBox
isn't closed when you put the cursor inside the TextBox
, but works wrong with the CheckBox
.
The quickest way to solve this issue is to change focus manually when a user clicks the CheckBox
.
I use the following sequence of steps:
1) Handle the GotFocus
event for every control in the application
2) Leave only events of the CheckBox
control
3) Check whether the CheckBox
is inside the current ComboBox
4) If yes, return focus to the ComboBox
public MainWindow()
{
InitializeComponent();
//...
comboBox.AddHandler(FrameworkElement.GotFocusEvent, (RoutedEventHandler)OnGotFocus);
}
private void OnGotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is CheckBox)
{
var comboBox = (ComboBox)sender;
var comboBoxItem = GetParentElement<ComboBoxItem>(e.OriginalSource);
if (comboBoxItem != null && comboBox.Items.OfType<object>().Select(comboBox.ItemContainerGenerator.ContainerFromItem).Contains(comboBoxItem))
comboBox.Focus();
}
}
private T GetParentElement<T>(object element) where T : DependencyObject
{
var current = element as DependencyObject;
while (current != null && !(current is T))
{
current = VisualTreeHelper.GetParent(current);
}
return current as T;
}
It is a quite messy solution, but anyway it works.
回答2:
What you need is the Check ComboBox control, which is part of the WPF extended toolkit. You can find it here: Check ComboBox
来源:https://stackoverflow.com/questions/8126570/combobox-with-checkboxes-closes-on-checkbox-click