繁体   English   中英

UserControl和KeyBinding(WPF)出现问题

[英]Issue with UserControl and KeyBinding (WPF)

我有一个带有ContentControl的窗口,在其中显示我的用户控件。 每个用户控件都有其键绑定。 问题在于,当用户控件失去焦点时,键绑定将无法正常工作。 如果单击用户控件外部窗口中的任意位置,则无权访问键盘绑定。 我可以使用全局键绑定,但是每个键绑定都是特定的用户控件? 我找到了一些解决方案,但没有为我服务。 谢谢!

您可以做什么:

  1. 创建包含命令的新类。 每个命令代表您要在用户控件内执行的操作

     public static class CustomCommands { public static readonly RoutedUICommand SettingsCommand = new RoutedUICommand ( "SettingsCommand", "SettingsCommand", typeof(CustomCommands), new InputGestureCollection() { } ); } 
  2. 在您的用户控制订阅中,此命令:

在XAML中:

<UserControl.CommandBindings>
    <CommandBinding Command="local:CustomCommands.SettingsCommand" Executed="OnSettingsExecuted"></CommandBinding>
</UserControl.CommandBindings>

或隐藏在代码中:

CustomCommands.SettingsCommand.Executed += (s, e) => { };
  1. 在窗口中,将键绑定到命令:

     <Window.InputBindings> <KeyBinding Key="S" Modifiers="Control" Command="local:CustomCommands.SettingsCommand" /> </Window.InputBindings> 

因此,当窗口处于活动状态时,您将具有窗口比例的键绑定。

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM