KeyDown event does not fire for 'enter' in AutoCompleteBox

I am using Silverlight user management in my ASP.NET web application. The user control has several autocomplete blocks, and it seems that the input key never fires the keydown event in any of them while it fires for the other keys.

I assume that the autocomplete fields should handle the input key in a different way, perhaps to select an item from the list. - Thus, it works with simple text fields.

I was thinking of overriding the event handler in the new output control ...

Have you found a solution for this?

+3
source share
4

, , . AutoCompleteBox Reflector, , Enter, Escape F4 - , e.Handled = true.

, PreviewKeyDown Silverlight.

, , - 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);
        }
    }
}

HandleKeyEvents XAML, :

<local:MyAutoCompleteBox HandleKeyEvents="False"/>

AutoCompleteBox - e.Handled = true , - . , , KeyDown ( Enter).

+4

- KeyDown, , KeyUp...:) orignal, , , !

+2

, , WPF (, , Silverlight) . .

The simplest solution is to hook PreviewKeyDown. That is pretty much what it is.

0
source

I based this solution on Dan Aucler, simplifying it to remove the dependency property and change the logic to reflect the use case that I think you are 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);
        }
    }
}
0
source

Source: https://habr.com/ru/post/1760290/


All Articles