1

I'm trying to create a UserControl that displays a multi-page image and allows the user to zoom, rotate, and browse an image. The one part I'm having a problem with is getting the keyboard shortcuts setup properly. I figured out I need to have the InputBindings setup on the Window class that is hosting the UserControl. I did figure out how to create an InputBinding in the code behind but I'm expecting a lot of keyboard shortcuts and I think having them in the XAML will be easier. Here is an example project that I'm testing this out in:

MainWindow.xaml

<Window x:Class="CommandTest.Window1"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
xmlns:local="clr-namespace:CommandTest"  
Title="Window1" Height="300" Width="344">
<Window.InputBindings>
    <KeyBinding
        Key="N"
        CommandTarget="x:UCon"
        />
</Window.InputBindings>

<StackPanel>
    <local:NestedControl x:Name="UCon">
    </local:NestedControl>
</StackPanel>
</Window>  

MainWindow.Xaml.cs

using System.Windows;
using System.Windows.Input;

namespace CommandTest
{
    public partial class Window1 : Window
    {
         public Window1()
         {
             InitializeComponent();
             KeyGesture keyg = new KeyGesture(Key.OemPlus, ModifierKeys.Control);

             KeyBinding kb = new KeyBinding((ICommand)UCon.Resources["Commands.ZoomOut"], keyg);

             kb.CommandTarget = UCon;

             this.InputBindings.Add(kb);   
         }

     }
}

UserControl1.xaml

<UserControl x:Class="CommandTest.NestedControl"  
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <UserControl.Resources>
        <!-- UI commands. -->        
        <RoutedCommand x:Key="Commands.ZoomOut" />        
    </UserControl.Resources>

    <UserControl.CommandBindings>
        <CommandBinding x:Name="ZoomCommand" Command="{StaticResource Commands.ZoomOut}" CanExecute="ZoomCommand_CanExecute" Executed="ZoomCommand_Executed"/>        
    </UserControl.CommandBindings>
    <Button Command="{StaticResource Commands.ZoomOut}">Zoom</Button>
</UserControl>  

UserControl1.xaml.cs

using System.Windows.Controls;
using System.Windows.Input;

namespace CommandTest
{
    public partial class NestedControl : UserControl
    {
        public NestedControl()
        {
            InitializeComponent();
        }

        private void ZoomCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void ZoomCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            System.Windows.MessageBox.Show("Zoom", "Zoom");
        }        
    }
}
4

1 に答える 1

0

キーをウィンドウにバインドする代わりに、コマンド自体にバインドします (カスタム コマンドの場合は、ZoomCommand のコンストラクターでこれを行うことができます)。

ZoomCommand.InputGestures.Add(new KeyGesture(Key.N));

次に、UserControl のコンストラクターで次のようにします。

Window.GetWindow(this).CommandBindings.Add(new CommandBinding(new ZoomCommand(), ZommCommand_handler));
于 2010-11-11T22:51:03.867 に答える