How to capture the event when WPF combobox item is click, or selected by Enter key?
I had tried this for few hours, but it's not working.
I have a combobox, with a few items in there, generated dynamically like a search box.
Now I want to capture an event, w开发者_开发百科hen user click on the dropdown menu item, or click on the dropdown menu item.
How to achieve this? I tried to set mouse/keyboard event handler on Combobox, but it only works on the combobox's textbox, not in the dropdown list.
Thanks.
Edit: I forgot to mention that I has custom DataTemplate on my Combobox. I tried another approach which set the event in ComboBox.ItemContainerStyle.
I tried PreviewKeyDown, but it is not captured. Any idea?
instead of using the MouseLeftButtonDown
event,
use the PreviewMouseLeftButtonDown
event
WPF supports the "event bubbling" concept, that when an event is fired, it bubbles up the an higher element on the tree that implements that event. but the ComboBox itself already implements the click event. so you have to tell it to bubble "down".
I think that what you are looking for is the "SelectionChanged" event. This event is raised as soon as you select an item in the drop down, either by mouse click or by navigating with arrow keys and hit "Enter" (I tried both with success).
<ComboBox x:Name="cbobox" ItemsSource="{Binding SourceList}"
SelectionChanged="cbobox_SelectionChanged">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<TextBlock Text="{Binding BusinessProperty}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
I also tried this for hours and here is my solution: Subscribe to the KeyUp event.
Somehow this is the only event being fired and that can be used to distinct between mouse and keyboard selection using custom templates.
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
KeyUp += OnKeyUp;
}
void OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{...}
else if (e.Key == Key.Up)
{...}
else if(e.Key == Key.Enter)
{...}
}
Hope this works for you as well.
If you are using a custom ControlTemplate for your ComboBoxItem, it might be a problem with the HorizontalContentAlignment of the ContentPresenter. Here is how my old ControlTemplate looked, when I was having the problem:
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
....
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
And here is how it looked after I fixed the problem:
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
....
<ContentPresenter
HorizontalAlignment="Stretch"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
Alternatively, you could leave the ControlTemplate alone and set the HorizontalContentAlignment for each ComboBoxItem. However, I felt like people shouldn't have to do that to make my ComboBoxItem ControlTemplate work.
After a week i end up with this
<StackPanel>
<ComboBox Name="cmb" ItemsSource="{Binding Items}"
SelectedValue="{Binding SelectedVale}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="{Binding DisplayText}" Command="{Binding ItemClick}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"></Button>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
For ComboBox in WPF, Use the GotFocus
Event
GotFocus
Event will fire if you use mouse or keyboard to access the combobox
Try setting an event of type DropDownClosed
and capture that.
I tested it in my program and it seems to be called just before closing combobox dropdown.
I refer you to this question for further information on this.
精彩评论