如何在单击 WPF 组合框项或按 Enter 键选择时捕获事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5658515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:47:27  来源:igfitidea点击:

How to capture the event when WPF combobox item is click, or selected by Enter key?

wpfcombobox

提问by VHanded

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, when 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.

编辑:我忘了提到我的 Combobox 上有自定义 DataTemplate。我尝试了另一种在 ComboBox.ItemContainerStyle 中设置事件的方法。

I tried PreviewKeyDown, but it is not captured. Any idea?

我试过 PreviewKeyDown,但它没有被捕获。任何的想法?

回答by Notter

instead of using the MouseLeftButtonDownevent, use the PreviewMouseLeftButtonDownevent

而不是使用MouseLeftButtonDown事件,使用PreviewMouseLeftButtonDown事件

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".

WPF 支持“事件冒泡”概念,即当一个事件被触发时,它会冒泡树上实现该事件的更高元素。但是 ComboBox 本身已经实现了点击事件。所以你必须告诉它“向下”冒泡。

回答by Bruno

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).

我认为您正在寻找的是“SelectionChanged”事件。只要您在下拉列表中选择一个项目,通过鼠标单击或使用箭头键导航并点击“Enter”(我都成功地尝试了两者),就会引发此事件。

       <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>

回答by Martin

I also tried this for hours and here is my solution: Subscribe to the KeyUp event.

我也尝试了几个小时,这是我的解决方案:订阅 KeyUp 事件。

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.

希望这也适用于您。

回答by Moumit

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>

回答by Amir Maleki

Try setting an event of type DropDownClosedand capture that.

尝试设置类型的事件DropDownClosed并捕获它。

I tested it in my program and it seems to be called just before closing combobox dropdown.

我在我的程序中测试了它,它似乎在关闭组合框下拉列表之前被调用。

I refer you to this questionfor further information on this.

我向您推荐这个问题以获取有关此的更多信息。

回答by Jean Libera

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:

如果您为 ComboBoxItem 使用自定义 ControlTemplate,则 ContentPresenter 的 Horizo​​ntalContentAlignment 可能有问题。这是我的旧 ControlTemplate 出现问题时的样子:

<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.

或者,您可以不使用 ControlTemplate 并为每个 ComboBoxItem 设置 Horizo​​ntalContentAlignment。但是,我觉得人们不应该这样做才能使我的 ComboBoxItem ControlTemplate 工作。