从父窗口绑定到 UserControl RoutedCommand

发布于 2024-10-01 16:29:11 字数 2696 浏览 0 评论 0原文

我正在尝试创建一个显示多页图像并允许用户缩放、旋转和浏览图像的用户控件。我遇到的问题是如何正确设置键盘快捷键。我发现我需要在托管 UserControl 的 Window 类上设置 InputBindings。我确实弄清楚了如何在后面的代码中创建 InputBinding,但我期望有很多键盘快捷键,并且我认为将它们放在 XAML 中会更容易。这是我正在测试的示例项目:

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");
        }        
    }
}

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");
        }        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-10-08 16:29:11

不要将键绑定到 Window,而是将其绑定到命令本身(如果这是自定义命令,您可以在 ZoomCommand 的构造函数中执行此操作):

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

然后,在 UserControl 的构造函数中:

Window.GetWindow(this).CommandBindings.Add(new CommandBinding(new ZoomCommand(), ZommCommand_handler));

Instead of binding the key to the Window, bind it to the command itself (You could do this in the constructor for ZoomCommand if this is a custom command):

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

Then, in the Constructor for the UserControl:

Window.GetWindow(this).CommandBindings.Add(new CommandBinding(new ZoomCommand(), ZommCommand_handler));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文