2016-04-17 14 views
0

で働いていない:私はMenuItemをクリックするとショートカットは、私が<code>CommandBindings</code>を持って<code>UserControl</code>を持っているユーザーコントロール

<UserControl x:Class="DbCreator.UserControls.Menu" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:DbCreator" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.CommandBindings> 
    <CommandBinding Command="SelectAll" Executed="SelectAllCommand_Executed"/> 
    <CommandBinding Command="local:MyCommands.ToggleNavigator" Executed="ToggleNavigatorCommand_Executed"/> 
    <CommandBinding Command="local:MyCommands.ToggleGrid" Executed="ToggleGridCommand_Executed"/> 
    <CommandBinding Command="local:MyCommands.SelectNone" Executed="SelectNoneCommand_Executed"/> 
</UserControl.CommandBindings> 
<Menu Background="White"> 
    <MenuItem Header="_Файл"/> 
    <MenuItem Header="_Правка"> 
     <MenuItem Header="Выделить всё" Command="SelectAll"/> 
     <MenuItem Header="Убрать выделение" Command="local:MyCommands.SelectNone"/> 
    </MenuItem> 
    <MenuItem Header="_Вид"> 
     <MenuItem x:Name="ToggleNavigator" Header="Скрыть навигатор" Command="local:MyCommands.ToggleNavigator"/> 
     <MenuItem x:Name="ToggleGrid" Header="Скрыть сетку" Command="local:MyCommands.ToggleGrid"/> 
    </MenuItem> 
    <MenuItem Header="_Диаграмма"/> 
    <MenuItem Header="_Справка"/> 
</Menu> 

、私のExecuted方法は解雇が、ショートカットが機能していません。このUserControlの内容がMainWindowコードの一部だった場合、すべてのショートカットが完璧に機能しました。しかし、別のファイルにメニューを定義したい。

答えて

0

したがって、問題は、Menuが焦点が合わず、入力を受け取らなかったことでした。解決策は、にMainWindowを追加することです。

<Window.InputBindings> 
    <KeyBinding Command="SelectAll" CommandTarget="{Binding ElementName=Menu}" Key="A" Modifiers="Control"/> 
    <KeyBinding Command="local:MyCommands.SelectNone" CommandTarget="{Binding ElementName=Menu}" Key="A" Modifiers="Control+Shift"/> 
    <KeyBinding Command="local:MyCommands.ToggleNavigator" CommandTarget="{Binding ElementName=Menu}" Key="N" Modifiers="Alt"/> 
    <KeyBinding Command="local:MyCommands.ToggleGrid" CommandTarget="{Binding ElementName=Menu}" Key="G" Modifiers="Alt"/> 
</Window.InputBindings> 
関連する問題