为什么 wpftoolkit 日期选择器会吃掉返回键按下事件?

发布于 2024-10-09 14:46:15 字数 745 浏览 0 评论 0原文

我想知道是否有人知道为什么日期选择器会将标准键传递给任何父控件的按键向下路由事件,而不是返回键?

这是我写的xaml:

    <WrapPanel Name="_wpParameters" 
               Grid.Row="0" Grid.Column="0" 
               Orientation="Horizontal" 
               Grid.IsSharedSizeScope="True"
               Keyboard.KeyDown="_wpParameters_KeyDown" >
        <!-- this is where the dynamic parameter controls will be added -->
    </WrapPanel>

这是我用来检查返回键的代码:

private void _wpParameters_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        RaiseEvent(new RoutedEventArgs(LoadLiveResultsEvent, this));
    }
}

我在意外时使用了按下键(本意是使用向上键),但我发现有趣的是标准数字和/字符正在触发逻辑,但不是返回键。任何想法为什么返回键不包含为按键键?

I am wondering if anybody knows why the datepicker will pass standard keys to any parent control's key down routed event, but not the return key?

here's the xaml i wrote:

    <WrapPanel Name="_wpParameters" 
               Grid.Row="0" Grid.Column="0" 
               Orientation="Horizontal" 
               Grid.IsSharedSizeScope="True"
               Keyboard.KeyDown="_wpParameters_KeyDown" >
        <!-- this is where the dynamic parameter controls will be added -->
    </WrapPanel>

Here is the code i was using to check for the return key:

private void _wpParameters_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        RaiseEvent(new RoutedEventArgs(LoadLiveResultsEvent, this));
    }
}

I was using key down on accident (meant to use key up) but I found it interesting that the standard numeric and / characters were firing the logic, but not the return key. Any Idea's why the return key is not included as a key down key?

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

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

发布评论

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

评论(2

杯别 2024-10-16 14:46:16

KeyDown 事件是一个较低级别的事件
可能无法正常运行的文本输入事件
正如某些控件的预期。这
是因为有些控件有控制权
合成或类处理
提供了更高级别的版本
文本输入处理及相关
事件。

正如在 MSDN 上查看的......我的假设是控件正在消耗事件,并​​且可能将文本提交到可绑定源和其他清理,然后将事件标记为已处理。

The KeyDown event is a lower-level
text input event that might not behave
as expected on certain controls. This
is because some controls have control
compositing or class handling that
provides a higher-level version of
text input handling and related
events.

As viewed on MSDN...my assumption is that the control is consuming the event and perhaps committing the text to the bindable source and other cleanup and then marking the event as handled.

痴骨ら 2024-10-16 14:46:16

另外不得不提一下我的解决方案。
我有一个父视图,它处理所有子视图模型中的 keyDown 事件。
我为 DatePicker、MaskedTextBox 等特殊控件声明了一种行为...捕获 PreviewKeyDown 隧道事件并引发 KeyDown 冒泡事件:

public class EnterPressedBehavior : Behavior<UIElement>
{
    public ICommand EnterPressedCommand { get; private set; }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewKeyDown += EnterPressed;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewKeyDown -= EnterPressed;
    }

    private void EnterPressed(object sender, KeyEventArgs keyEventArgs)
    {
        if (Keyboard.PrimaryDevice != null && Keyboard.PrimaryDevice.ActiveSource != null)
        {
            var eventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyEventArgs.Key) { RoutedEvent = UIElement.KeyDownEvent };

            AssociatedObject.RaiseEvent(eventArgs);
        }
    }
}

此行为分配给 datePicker:

<DatePicker x:Name="BirthDateDatePicker" Grid.Column="1"
                    Grid.Row="6" Margin="3" HorizontalAlignment="Stretch"                                              
                    IsEnabled="{Binding PersonFieldsEditDenied}"
                    Validation.ErrorTemplate="{StaticResource DefaultValidationTemplate}"
                    AutomationProperties.AutomationId="BirthDateDatePicker">
            <i:Interaction.Behaviors>
                <viewModels:EnterPressedBehavior />
            </i:Interaction.Behaviors>
</DatePicker>

由父视图侦听:

<Window
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title=""
       KeyDown="OnKeyDownHandler">

代码隐藏:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            // your code
        }
    }

In addition had to mention my solution.
I had a parent view, that handle the keyDown event from all child viewmodels.
I declared a behavior for special controls like DatePicker,MaskedTextBox,etc... that catch previewKeyDown tunneling event and raise KeyDown bubbling event:

public class EnterPressedBehavior : Behavior<UIElement>
{
    public ICommand EnterPressedCommand { get; private set; }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewKeyDown += EnterPressed;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewKeyDown -= EnterPressed;
    }

    private void EnterPressed(object sender, KeyEventArgs keyEventArgs)
    {
        if (Keyboard.PrimaryDevice != null && Keyboard.PrimaryDevice.ActiveSource != null)
        {
            var eventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, keyEventArgs.Key) { RoutedEvent = UIElement.KeyDownEvent };

            AssociatedObject.RaiseEvent(eventArgs);
        }
    }
}

this behavior assigned to datePicker:

<DatePicker x:Name="BirthDateDatePicker" Grid.Column="1"
                    Grid.Row="6" Margin="3" HorizontalAlignment="Stretch"                                              
                    IsEnabled="{Binding PersonFieldsEditDenied}"
                    Validation.ErrorTemplate="{StaticResource DefaultValidationTemplate}"
                    AutomationProperties.AutomationId="BirthDateDatePicker">
            <i:Interaction.Behaviors>
                <viewModels:EnterPressedBehavior />
            </i:Interaction.Behaviors>
</DatePicker>

which is listened by parent view:

<Window
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title=""
       KeyDown="OnKeyDownHandler">

Code behind:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            // your code
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文