【问题标题】:KeyBinding two commands (one in the VM and one in the View) to one keyKeyBinding 两个命令(一个在 VM 中,一个在 View 中)到一个键
【发布时间】:2012-07-04 10:32:08
【问题描述】:

我有一个如下所示的文本框:

<TextBox Text="{Binding Stuff, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding Path=LoadStuffCommand}" />
        <KeyBinding Key="Enter" Command="SelectAll"  />
    </TextBox.InputBindings>
</TextBox>

当它运行时,只运行 SelectAll 命令。但是,如果我取出 SelectAll 行,则 LoadStuffCommand 运行良好。

显然它正在用下​​一个命令替换命令(最后一个获胜)。但我需要一种方法来运行我的命令并选择 TextBox 中的所有文本。

通常,我会发出另一个命令来触发我的两个命令。但其中之一在我的 ViewModel 中,一个在我的视图中。我怎样才能发出一个同时触发两者的命令? (有没有办法在 XAML 中创建命令?)

【问题讨论】:

标签: .net wpf xaml binding mvvm


【解决方案1】:

这可以通过an aggregator 完成。基本上,将命令分组到一个按顺序调用它们的对象中,并像一个命令一样绑定到该对象。

【讨论】:

  • 感谢您的回答。但是,该示例使用自定义滚动的命令(因此它们具有“实例”属性)或应用程序级命令。我只是在 ViewModel 上使用 DelegateCommand 或在 TextBox 上使用 SelectAll 命令。为此滚动我自己的命令类似乎是错误的方法。
  • DelegateCommands 实现 Icommand。您应该能够在不更改任何内容的情况下使用此解决方案。或者使用这个模型滚动你自己的。它非常简单。
猜你喜欢
  • 2012-04-10
  • 2016-08-24
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 2016-01-19
相关资源
最近更新 更多