我有一个MainView (窗口)作为一个shell,当用户导航应用程序时,我在其中加载不同的UserControls,其中包含不同的“页面”或“视图”。
我通过将ViewModel设置为MainView上的Content属性来更改视图。DataTemplate根据ViewModel选择正确的视图。
我想使用F1键盘快捷方式来触发当前UserControl上的命令。
如果我在InputBindings上添加UserControl,我必须在UserControl中选择一些东西,然后才能点击F1。
如果在窗口中添加InputBindings,则没有正确的DataContext (ViewModel)。我甚至尝试将CommandTarget设置为ViewModel的Content属性。
<Window.InputBindings>
<KeyBinding Key="F1" CommandTarget="{Binding ElementName=ContentControl}" Command="{Binding ShowInfoCommand}" />
</Window.InputBindings>
我还尝试将FocusManager.FocusedElement=“{bindingElementName=ContentControl}”设置在MainWindow上,在UserControl上设置Focusable=true,甚至在ContentControl上设置。
发布于 2014-10-28 10:48:12
我在这里找到了一个很好的解决方案,at SO是由Adi Lester提供的。他使用一种行为将密钥绑定从UserControl传播到窗口。
我发现,如果在generic.xaml中使用泛型内容模板中的行为,则必须通过TemplatedParent而不是TemplateBinding绑定命令
<Border behaviors:InputBindingBehavior.PropagateInputBindingsToWindow="True">
<Border.InputBindings>
<KeyBinding Key="F1" Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ShowInfoCommand}" />
</Border.InputBindings>
</Border>
https://stackoverflow.com/questions/26605955
复制相似问题