从父窗口绑定到 UserControl RoutedCommand
我正在尝试创建一个显示多页图像并允许用户缩放、旋转和浏览图像的用户控件。我遇到的问题是如何正确设置键盘快捷键。我发现我需要在托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将键绑定到 Window,而是将其绑定到命令本身(如果这是自定义命令,您可以在 ZoomCommand 的构造函数中执行此操作):
然后,在 UserControl 的构造函数中:
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):
Then, in the Constructor for the UserControl: