【发布时间】:2013-05-04 02:58:21
【问题描述】:
我有一个 MVVM 应用程序,其中 MainWindow 包含一个带有多个视图的网格。
在其中一个视图模型中有一个命令,我可以通过在其相应视图中使用热键来激活它。只有当我被放置在包含特定视图的 MainWindow 部分时,我才能激活该命令。
如果我只想在特定视图中激活命令,以下代码可以正常工作:
ComponentView.xaml:
...
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</UserControl.InputBindings>
</UserControl>
我希望能够使用主窗口任何部分的热键来激活该命令。
这是我在主窗口中进行键绑定的失败尝试,以便可以从任何地方激活命令:
MainWindow.xaml:
<Window x:Class="MyProgram.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:MyProgram.View"
xmlns:vm="clr-namespace:MyProgram.ViewModel"
...>
<Grid>
// Grid content
</Grid>
<Window.DataContext>
<vm:ComponentViewModel>
<KeyBinding Gesture="CTRL+U" Command="{Binding Path=UploadCmd}"/>
</vm:ComponentViewModel>
</Window.DataContext>
</Window>
是否有可能以某种方式让应用程序知道命令在 xaml 中的直接位置?
【问题讨论】:
标签: wpf xaml mvvm datacontext key-bindings