WPF MVVM KeyBinding 无法立即识别并且并不总是有效

2023-11-24

无论出于何种原因,当我的 WPF 应用程序加载时,我的 UserControl 的 KeyBindings 就无法工作。它们在我按下表单上的按钮后起作用,但当我通过单击或 alt tab 键或移动或类似方式将焦点设置到表单时则不起作用。当它们工作时,我的输入键会打印一个随机数字。 (有时 5 个,有时 7 个等...)。

<UserControl x:Class="WpfCalculator.View.CalculatorView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300"
    >
<UserControl.InputBindings>
    <KeyBinding Key="DELETE" Command="{Binding Path=IBackspaceOnInput}" />
    <KeyBinding Key="BACKSPACE" Command="{Binding Path=IBackspaceOnInput}" />

    <KeyBinding Key="NUMPAD0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" />
    <KeyBinding Key="NUMPAD1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" />
    <KeyBinding Key="NUMPAD2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" />
    <KeyBinding Key="NUMPAD3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" />
    <KeyBinding Key="NUMPAD4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" />
    <KeyBinding Key="NUMPAD5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" />
    <KeyBinding Key="NUMPAD6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" />
    <KeyBinding Key="NUMPAD7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" />
    <KeyBinding Key="NUMPAD8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" />
    <KeyBinding Key="NUMPAD9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" />

    <KeyBinding Key="D0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" />
    <KeyBinding Key="D1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" />
    <KeyBinding Key="D2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" />
    <KeyBinding Key="D3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" />
    <KeyBinding Key="D4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" />
    <KeyBinding Key="D5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" />
    <KeyBinding Key="D6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" />
    <KeyBinding Key="D7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" />
    <KeyBinding Key="D8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" />
    <KeyBinding Key="D9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" />

    <KeyBinding Key="ADD" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" />
    <KeyBinding Key="SUBTRACT" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" />
    <KeyBinding Key="MULTIPLY" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" />
    <KeyBinding Key="DIVIDE" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" />

    <KeyBinding Key="Return" Command="{Binding Path=ICalculateExpression}" CommandParameter="" />
    <KeyBinding Key="Enter" Command="{Binding Path=ICalculateExpression}" CommandParameter="" />
    <KeyBinding Key="Escape" Command="{Binding Path=IClearInput}" CommandParameter="" />

    <KeyBinding Gesture="CTRL+M" Command="{Binding Path=IRememberExpression}" CommandParameter="" />
    <KeyBinding Gesture="CTRL+R" Command="{Binding Path=IRecallExpression}" CommandParameter="" />
    <KeyBinding Gesture="CTRL+X" Command="{Binding Path=IForgetExpression}" CommandParameter="" />

    <KeyBinding Key="Left" Command="{Binding Path=IMMoveCursor}" CommandParameter="1" />
    <KeyBinding Key="Right" Command="{Binding Path=IMMoveCursor}" CommandParameter="-1" />

</UserControl.InputBindings>

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="305" Width="489">
    <TextBox Name="input" HorizontalAlignment="Right"  IsReadOnly="True" Margin="0,12,200,271" Text="{Binding Path=UserInput}" Width="275" />
    <Button Content="CE" Margin="143,0,323,147" Command="{Binding Path=IBackspaceOnInput}" CommandParameter="" Height="23" VerticalAlignment="Bottom" />
    <Button Content="1" Height="23" HorizontalAlignment="Right" Margin="0,106,294,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" />
    <Button Content="2" Height="23" HorizontalAlignment="Right" Margin="0,106,265,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" />
    <Button Content="3" Height="23" HorizontalAlignment="Right" Margin="0,106,236,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" />
    <Button Content="4" Height="23" HorizontalAlignment="Right" Margin="0,77,294,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" />
    <Button Content="5" Height="23" HorizontalAlignment="Right" Margin="0,77,236,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" />
    <Button Content="6" Height="23" HorizontalAlignment="Right" Margin="0,77,265,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" />
    <Button Content="7" Height="23" HorizontalAlignment="Right" Margin="0,48,294,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" />
    <Button Content="8" Height="23" HorizontalAlignment="Right" Margin="0,48,265,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" />
    <Button Content="9" Height="23" HorizontalAlignment="Right" Margin="0,48,236,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" />
    <Button Content="0" Height="23" HorizontalAlignment="Left" Margin="201,135,0,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" />

    <Button Content="+" Height="23" HorizontalAlignment="Right" Margin="0,48,201,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" />
    <Button Content="-" Height="23" HorizontalAlignment="Right" Margin="0,77,201,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" />
    <Button Content="*" Height="23" HorizontalAlignment="Right" Margin="0,106,201,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" />
    <Button Content="/" Height="23" HorizontalAlignment="Right" Margin="0,135,201,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" />
    <Button Content="." Height="23" HorizontalAlignment="Right" Margin="0,135,236,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" />

    <Button Content="C" HorizontalAlignment="Right" Margin="0,164,323,118" Width="23" Command="{Binding Path=IClearInput}" CommandParameter="" />
    <Button Content="MC" Height="23" HorizontalAlignment="Right" Margin="0,0,323,234" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IForgetExpression}" CommandParameter=""/>
    <Button Content="M+" Height="23" HorizontalAlignment="Right" Margin="0,0,323,176" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRememberExpression}" CommandParameter=""/>
    <Button Content="MR" Height="23" HorizontalAlignment="Right" Margin="0,0,323,205" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRecallExpression}" CommandParameter=""/>

    <Expander ExpandDirection="Left" Header="History" Height="91" HorizontalAlignment="Right" Margin="0,193,200,0" VerticalAlignment="Top" Width="275">
        <ListView ItemsSource="{Binding Path=History}" >
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="Start" DisplayMemberBinding="{Binding Started}"/>
                    <GridViewColumn Header="End" DisplayMemberBinding="{Binding Completed}"/>
                    <GridViewColumn Header="Expression" DisplayMemberBinding="{Binding Calculation}"/>
                    <GridViewColumn Header="Solution" DisplayMemberBinding="{Binding Result}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Expander>
    <Button Command="{Binding Path=ICalculateExpression}" CommandParameter="" IsEnabled="{Binding Path=IsEqualsBtnEnabled}" Content="=" Height="23" Margin="172,0,236,118" VerticalAlignment="Bottom" />

</Grid></UserControl>

我真的没有遇到过其他人遇到过这个特殊的问题,所以我真的不知道除了这个之外还能提供什么。请告诉我是否需要任何其他信息?


您是否验证过该控件在加载时确实具有焦点Mole?无论您的父控件是什么,都可能会保持焦点,直到手动选择按钮为止。至于 Enter 键,听起来好像它们可能正在选择最后单击的按钮,因为它仍然具有焦点而不是触发命令。

您可能需要查看您的命令,因为我不确定声明是否设置正确。为了KeyBindings,您的命令应在 XAML 中引用为CommandReference, like 本文描述。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF MVVM KeyBinding 无法立即识别并且并不总是有效 的相关文章

随机推荐

  • 将 iPad 内容输出到外部显示器

    我听说可以将 iPad 应用程序的内容输出到外部显示器 但应用程序必须为此做好准备 并且存在严重的限制 有什么指点吗 另外 iPhone 也能做到这一点吗 是一样的吗 几乎没有表明 您需要创建一个新的 UIWindow 并将其附加到 UIS
  • 下划线方法前缀

    我一直在检查 CodeIgniter 和 CakePHP 的代码 我注意到它们类中的一些方法带有下划线前缀 或双下划线 这样做的目的是什么 如果不是以下任一情况PHP 魔法方法 是为了表示能见度缺乏适当的可见性关键字 蛋糕编码约定 由于我们
  • 更改fiddler代理服务器的用户名和密码[关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我正在使用 Fiddler 设置代理服务器 但在规则菜单下启用 需要代理授权 时 用户名 密码始终为 1 如何更改用户名 密码 我尝试更改 oSession X AutoAuth 用
  • 如何读取3gp / AMR-NB音频格式的原始值?

    在我的 Android 应用程序中 我正在录制用户的声音 并将其保存为 3gp 编码的音频文件 我想做的就是打开它 即代表音频样本的序列 x n 以便执行一些音频信号分析 有谁知道我该怎么做 您可以使用安卓媒体编解码器解码 3gp 或其他媒
  • Android 中的 Button 类膨胀错误

    我有一个最小 sdk 16 到 23 的应用程序 我想尽可能多地使用 Material design 它还必须是全屏应用程序 包含 AppCompat 支持库 现在我有一些按钮的登录活动
  • 如何自动更新 Windows Mobile 应用程序

    我有一个 net cf 3 5 Windows Mobile 应用程序 我的客户希望它具有自动更新功能 这是我到目前为止所拥有的 使用智能设备 CAB 项目创建 CAB 这是否足够好 或者我应该在这里做其他事情 2 获取应用程序版本号 As
  • iOS - 关闭作为 inputView 呈现的 UIDatePicker

    我的 UI 中有一个文本字段 当选择它时会显示 UIDatePicker 而不是默认键盘 我如何设置一个按钮以便在用户完成后关闭选择器 我所做的是将我的 inputView 作为自定义视图 其中包含UIDatePicker其上方有一个工具栏
  • $this->load->model() 在 CodeIgniter 中不起作用

    我正在使用 CodeIgniter 2 1 2 这是我现在的情况 我有一个名为 math php 的模型C wamp www cr8v application models 我正在尝试将其加载到我的控制器中C wamp www cr8v a
  • Ajax文件下载问题

    我正在我的应用程序中下载动态文件 使用 iframe 模拟 ajax 我正在做的是 当发出下载请求时 我将创建一个动态的不可见 iframe 并将 iframe 的 src 设置为下载网址 我能够成功下载文件 但要求是显示下载一旦下载开始
  • jQuery 1.10.1 在选择上设置不存在的值

    有人可以解释一下这种行为
  • Java内存模型中的Happens-Before关系

    关于 JLS ch17线程和锁 它表示 如果一个操作发生在另一个操作之前 则第一个操作对第二个操作可见并且在第二个操作之前排序 我想知道 1 之前订购 到底是什么意思 因为即使action a发生在action b之前 在某些实现中acti
  • Bootstrap 3+Rails 4 - 某些 Glyphicons 不工作

    我正在尝试在我的 Rails 4 应用程序中使用 Bootstrap 3 已关注this使用 bootstrap saas 设置 bootstrap 3 的教程thisgithub 页面 Bootstrap 工作正常 但字形图标未按预期工作
  • C# 如何正确地对遵循装饰器模式的类进行单元测试?

    我对单元测试相当陌生 我们说话时我实际上正在研究它 我的目标当然是能够在下面的类中测试该方法 该类只是检查输入是否已经在缓存中 如果输入不在缓存中 它将返回输入的反转形式 虽然实现不在这里 但假设它存在 因为目的只是为了测试 基本上 目标是
  • 获取与 int 值关联的枚举

    以前 我将 LegNo 枚举简单定义为 NO LEG LEG ONE LEG TWO 并通过调用return LegNo values i 我能够获得与每个枚举相关的值 但现在我决定我想要LegNo enum NO LEG为 int 1 而
  • 如何传递列表元素作为引用?

    我将列表的单个元素传递给函数 我想修改该元素 从而修改列表本身 def ModList element element TWO l list l append one l append two l append three print l
  • 在 iOS/Swift 中创建并导出为 Base64 的 RSA 公钥在 Java 中无法识别

    TL DR 在 iOS 中生成并存储在钥匙串中 导出为 base64 并发送到 java 后端的 RSA 公钥无法识别 我正在 iOS 应用程序中实现聊天加密功能 并使用对称 非对称密钥来处理它 无需过多讨论细节 在后端 我使用用户的公钥来
  • 将.net core 5.0发布到单个exe文件中

    有谁能够帮助我 我正在尝试将我的 net core 控制台应用程序发布到单个文件中 我正在使用这个命令 dotnet publish r win x64 c Release p PublishSingleFile true p Publis
  • 如何在不登录 Xcode 8 的情况下构建 IPA

    我已经在SO和其他地方进行了搜索 我只发现这个答案的旧版本似乎不再起作用 还有大量其他内容甚至与最新版本不相关 长话短说 我们有一位开发人员向我们提供了 IPA 但他不想加入我们的开发人员小组 我告诉他给我们发送一个未签名的 IPA 但我们
  • 如何访问 Windows 中的特殊目录?

    在 Windows 上检索用户桌面 文档文件夹和其他系统文件夹的 C 语法是什么 您可以使用环境 GetFolderPath与环境 特殊文件夹枚举 例如 string desktopPath Environment GetFolderPat
  • WPF MVVM KeyBinding 无法立即识别并且并不总是有效

    无论出于何种原因 当我的 WPF 应用程序加载时 我的 UserControl 的 KeyBindings 就无法工作 它们在我按下表单上的按钮后起作用 但当我通过单击或 alt tab 键或移动或类似方式将焦点设置到表单时则不起作用 当它