【问题标题】:KeyDown event binding to user controlKeyDown 事件绑定到用户控件
【发布时间】:2015-04-06 06:13:46
【问题描述】:

我目前正在使用 Caliburn.Micro 处理 WPF 项目,并希望将 KeyDown 事件绑定到 UserControl。如果Window 被打开并且用户按下任何按钮,它应该被触发。

<UserControl x:Class="Test.Views.AppView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:cal="http://www.caliburnproject.org"
         cal:Message.Attach="[Event KeyDown] = [TestMethod($executionContext)]" />

很遗憾,这段代码不起作用。是否可以将Event 绑定到UserControl,而不是绑定到TextBoxButton 等特定控件?

【问题讨论】:

标签: c# wpf mvvm caliburn.micro


【解决方案1】:

如果此 UserControl 上的控件具有焦点,它将首先接收 KeyDown 事件并进行处理。这将阻止 UserControl 接收它。

使用 PreviewKeyDown 捕获事件。 Preview... 事件正是针对这种情况的。它们从根控件冒泡到子控件,而常规事件则向下延伸。

如果冒泡和隧道应该停止,不要忘记在处理程序的末尾设置e.Handled = true;

【讨论】:

    猜你喜欢
    • 2012-09-24
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多