【问题标题】:Binding to other controlls InputBindings绑定到其他控件 InputBindings
【发布时间】:2017-11-02 07:51:15
【问题描述】:

我想将我的ButtonKeybindings 绑定到我的WindowKeybindings

我的窗口 (Name="Mainwindow) 输入绑定:

<Window.InputBindings>
    <KeyBinding Key="Return" Command="{Binding KeyPressed}" CommandParameter="Next"/>
</Window.InputBindings>

我对按钮应该是什么样子的想法

<Button Content="Select Directory" Command="{Binding BrowseDirectory}">
     <Button.InputBindings>
           <KeyBinding Key="Return" Command="{Binding ElementName=Mainwindow, Path=InputBindings}"/>
      </Button.InputBindings>
</Button>

遗憾的是,Outwindow 没有显示任何类型的 Binderror 任何其他内容,以便让我了解它为什么不起作用。
所以基本上问题是:如何将Control.InputBindings 绑定到另一个Control.InputBindings

【问题讨论】:

  • 如果你在窗口上有键绑定,那么当按下正确的键时就会触发命令,在按钮上复制它会浪费时间
  • 当按钮获得焦点时,返回按钮将触发 button.clicked 事件 --> 它将开始执行命令。我正在寻找一种方法来克服这个问题,现在我决定将 中的内容复制粘贴到 。不漂亮,但在我找到时间寻找合适的解决方案之前完成了这项工作。

标签: c# wpf binding inputbinding


【解决方案1】:

为什么不直接将Command 属性绑定到同一个源属性:

<Button Content="Select Directory" Command="{Binding BrowseDirectory}">
    <Button.InputBindings>
        <KeyBinding Key="Return" Command="{Binding Path=DataContext.KeyPressed, RelativeSource={RelativeSource AncestorType=Window}}"/>
    </Button.InputBindings>
</Button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多