如何使用WPF组合框中的向下箭头键停止自动选择? [英] How do I stop auto-selection with down arrow key in a WPF combobox?

查看:88
本文介绍了如何使用WPF组合框中的向下箭头键停止自动选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于ComboBox的搜索控件。控件根据Co​​mboBox文本部分的内容过滤内容。



除非我按下向下箭头键,否则它会按照我想要的方式工作,在这种情况下,自动选择已过滤列表中的第一项 - 我不想要发生这种情况。



你认为这是一个简单的案例,可以捕获关键事件并将其标记为已处理但是无法正常工作。



我的ComboBox看起来像这样:



I have a search control based around a ComboBox. The control filters it's content according to the content of the text part of the ComboBox.

It all works as I want it to unless I hit the down arrow key in which case the first item in the filtered list is automatically selected - I don't want this to happen.

You'd think it would be a simple case of trapping the key event and marking it as handled but that's not working.

My ComboBox looks like this:

<ComboBox Name="cbUsers" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="0" IsEditable="True" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:Recipients, AncestorLevel=1}, Path=FilteredUsers}" IsTextSearchEnabled="False"  Focusable="True"   ScrollViewer.CanContentScroll="True" TextBoxBase.TextChanged="cbUsers_TextChanged" SelectionChanged="cbUsers_SelectionChanged" ToolTip="Begin typing name or enter initials to find user.">
</ComboBox>





我尝试了什么:



我试过添加一个PreviewKeyUp来如果键是向下箭头,则ComboBox并将e.Handled设置为true。这会激发但没有效果。



我也尝试直接在TextBox部分添加处理程序:





What I have tried:

I've tried adding a PreviewKeyUp to the ComboBox and setting e.Handled to true if the key is the down arrow. This fires but has no effect.

I've also tried adding a handler directly to the TextBox portion:

TextBox textBox = (TextBox)cbUsers.Template.FindName("PART_EditableTextBox", cbUsers);
textBox.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(textBox_PreviewKeyUp);





再次激活但不会停止自动选择。



Again this fires but doesn't stop the automatic selection.

推荐答案

将委托添加到预览事件的原因是它是一个多播委托 - 它们都被解雇了。通过路由事件,您可以通过设置要处理的事件参数来阻止其他事件触发。



如果您不熟悉路由策略,请查看这些事件,但是在预览关键事件的情况下,路由策略是隧道。这意味着如果您的ComboBox附加到关键事件,它的事件将首先触发。



果然,如果您在按下向下箭头时预览按键事件,它将永远不会被开除。正在预览该事件,识别它是向下箭头,并将处理设置为true。在这种情况下通常是正确的吗?



奇怪的是我无法通过ComboBox级别的模板来阻止事件。不确定我是否错过了什么,但我为Window添加了一个预览键事件,它截获它就好了。最好将ComboBox放在另一个容器中并在该级别进行拦截,但是如果你使用这样的东西它会有所帮助:



The reason why adding a delegate onto the preview up event is it's a multicast delegate - they all get fired. With routed events you can stop other events from firing by setting the event args to handled.

If you're not familiar with routing strategies look those up briefly, but in the case of preview key events the routing strategy is tunneling. That means if your ComboBox is attached to the key events it's events will fire first.

Sure enough if you preview the key up event on hitting a down arrow it will never get fired. Something is previewing that event, recognizing it's a down arrow, and setting handled to true. Makes sense in this case normally right?

Oddly I couldn't get it to stop the event with the template at the ComboBox level. Not sure if I missed something, but I added a preview key event for the Window and it intercepted it just fine. It would be best to put it the ComboBox inside another container and intercept at that level, but if you use something like this it can help:

if (e.Source is ComboBox && e.Key == Key.Down)
{
    e.Handled = true;
}





注意:在容器中调试预览键事件时,我在继续之后抛出错误 - 奇。仍然工作......



NOTE: When debugging the preview key events in a container I was tossing errors after continuing - odd. Still worked though...


这篇关于如何使用WPF组合框中的向下箭头键停止自动选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆