输入绑定而不是 PreviewKeyDown

发布于 2024-11-11 01:25:38 字数 1028 浏览 3 评论 0原文

在我的代码的 MainView 中,我有此事件来捕获 keyDown:

#region Constructor
PreviewKeyDown += SoftKeyMainPreviewKeyDown;
#endregion

private void SoftKeyMainPreviewKeyDown(object sender, KeyEventArgs e)
{
        var focusedElement = Keyboard.FocusedElement;
        switch (e.Key)
        {
            case Key.Up:
                DoSomething();
                break;
            .....
        }
}

现在我想将其移至 XAML 中的 InputBindings 中。

我的第一次尝试:

<UserControl.InputBindings>
    <KeyBinding Gesture="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
    ....
</UserControl.InputBindings>

CodeBehind:

public ICommand UpCommand { get; set; }

UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } };

没有任何反应。最有可能的是 KeyDown 事件由子元素处理。

是否可以在InputBindings的XAML中设置previewkeydown?如何实现这一目标?

In the MainView of my Code, i had this event to catch keyDown:

#region Constructor
PreviewKeyDown += SoftKeyMainPreviewKeyDown;
#endregion

private void SoftKeyMainPreviewKeyDown(object sender, KeyEventArgs e)
{
        var focusedElement = Keyboard.FocusedElement;
        switch (e.Key)
        {
            case Key.Up:
                DoSomething();
                break;
            .....
        }
}

Now I want to move that into InputBindings in the XAML.

My First try:

<UserControl.InputBindings>
    <KeyBinding Gesture="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
    ....
</UserControl.InputBindings>

CodeBehind:

public ICommand UpCommand { get; set; }

UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } };

With this nothing happens. Most probably the KeyDown-Event is handled by an Child-Element.

Is it posible to set previewkeydown in XAML in InputBindings? And how can this be achieved?

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

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

发布评论

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

评论(1

忘年祭陌 2024-11-18 01:25:38

您是否尝试过使用 Key 属性而不是 Gesture ?像这样:

<UserControl.InputBindings>
    <KeyBinding Key="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
    ....
</UserControl.InputBindings>

Have you tried using the Key property instead of Gesture? Like this:

<UserControl.InputBindings>
    <KeyBinding Key="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
    ....
</UserControl.InputBindings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文