KeyDown 事件不会因“enter”而触发;在自动完成框中

发布于 2024-09-14 17:25:57 字数 217 浏览 9 评论 0原文

我在 ASP.NET Web 应用程序中使用 Silverlight 用户控件。 用户控件有几个自动完成框,似乎回车键永远不会在其中任何一个中触发 keydown 事件,而它会触发其他键。

我假设自动完成框必须以不同的方式处理输入键,也许是为了从列表中选择一个项目。 - 因此它适用于简单的文本框。

我正在考虑在新的派生控件中覆盖事件处理程序...

你们找到解决方案了吗?

I am using a Silverlight usercontrol in my ASP.NET web app.
The user control has several autocomplete boxes and it seems that the enter key never fires the keydown event in any of them while it fires for other keys.

I'm assuming that autocomplete boxes must handle the enter key in a different way, perhaps for chosing an item from the list. - Thus it works with simple text boxes.

I was thinking about overriding the eventhandler in a new deriving control...

Have you guys found a solution for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃气十足 2024-09-21 17:25:58

Joe White 是正确的,某些控件自行处理关键事件,这具有将它们屏蔽到更高级别控件的效果。如果您查看 Reflector 中的 AutoCompleteBox,您将看到 Enter、Escape 和 F4 都会导致某些事情发生并标记 e.Handled = true。

不幸的是,Silverlight 世界中不存在 PreviewKeyDown。

我能够防止控件响应和捕获这些关键事件的一种方法是对控件进行子类化并重写 OnKeyDown 方法。像这样的东西将允许您控制控件是否对按键事件做出反应:

public class MyAutoCompleteBox : AutoCompleteBox
{
    public static readonly DependencyProperty HandleKeyEventsProperty = DependencyProperty.Register(
        "HandleKeyEvents",
        typeof(bool),
        typeof(MyAutoCompleteBox),
        new PropertyMetadata(true));

    public bool HandleKeyEvents
    {
        get { return (bool)GetValue(HandleKeyEventsProperty); }
        set { SetValue(HandleKeyEventsProperty, value); }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (this.HandleKeyEvents)
        {
            base.OnKeyDown(e);
        }
    }
}

然后您可以使用 XAML 中的 HandleKeyEvents 属性来禁用控件处理它们:

<local:MyAutoCompleteBox HandleKeyEvents="False"/>

这种类型的事情将阻止基本 AutoCompleteBox 标记 e .Handled = true 并允许事件冒泡,以便您的更高级别控件可以用它做其他事情。如果您想防止其他 KeyDown 事件(除了 Enter 之外)被破坏,您可以更具体地了解处理哪些键。

Joe White is correct that some controls handle key events on their own, which has the effect of masking them to higher-level controls. If you take a look at the AutoCompleteBox in Reflector you will see that Enter, Escape, and F4 all cause something to happen and mark e.Handled = true.

Unfortunately PreviewKeyDown does not exist in the Silverlight world.

One way I have been able to prevent controls from responding to and capturing these key events is by subclassing the control and overriding the OnKeyDown method. Something like this would allow you to control whether or not the control reacts to the key events:

public class MyAutoCompleteBox : AutoCompleteBox
{
    public static readonly DependencyProperty HandleKeyEventsProperty = DependencyProperty.Register(
        "HandleKeyEvents",
        typeof(bool),
        typeof(MyAutoCompleteBox),
        new PropertyMetadata(true));

    public bool HandleKeyEvents
    {
        get { return (bool)GetValue(HandleKeyEventsProperty); }
        set { SetValue(HandleKeyEventsProperty, value); }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (this.HandleKeyEvents)
        {
            base.OnKeyDown(e);
        }
    }
}

You could then use the the HandleKeyEvents property in XAML to disable the control from handling them:

<local:MyAutoCompleteBox HandleKeyEvents="False"/>

This type of thing would prevent the base AutoCompleteBox from ever marking e.Handled = true and allow the event to bubble so your higher-level control could do something else with it. You could get more specific with which keys were handled if you wanted to prevent other KeyDown events (besides Enter) from breaking.

千寻… 2024-09-21 17:25:58

小学生错误 - 我太专注于 KeyDown,以至于忘记了我可以使用 KeyUp 来代替......:)
它仍然没有回答我原来的问题,但至少我可以继续前进!

Schoolboy error - I was so focused on KeyDown that I forgot I could use KeyUp instead... :)
It still doesn't answer my orignal question, but at least I could move on!

毁虫ゝ 2024-09-21 17:25:58

我不知道自动完成框,但我知道一些 WPF 控件(可能还有一些 Silverlight 控件)自己处理一些键,并将它们标记为已处理。所以通常你不会看到这些事件。

最简单的解决方案是挂钩 PreviewKeyDown。这几乎就是它的用途。

I don't know about the autocomplete box, but I know that some WPF controls (and probably some Silverlight controls as well) handle some keys on their own, and mark them as Handled. So normally you won't see those events.

The simplest solution is to hook PreviewKeyDown instead. This is pretty much what it's there for.

七堇年 2024-09-21 17:25:58

我基于 Dan Auclair 的解决方案,简化了它以删除依赖属性并更改逻辑以反映我认为您正在寻找的用例。

public class MyAutoCompleteBox : AutoCompleteBox
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Enter && !IsDropDownOpen)
        {
            e.Handled = false;
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
}

I based this solution off of Dan Auclair's, simplifying it to remove the dependency property and changing the logic to reflect the use case I think you're looking for.

public class MyAutoCompleteBox : AutoCompleteBox
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Enter && !IsDropDownOpen)
        {
            e.Handled = false;
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文