【问题标题】:Bind to a UserControl RoutedCommand from parent window从父窗口绑定到 UserControl RoutedCommand
【发布时间】:2010-11-11 21:54:46
【问题描述】:

我正在尝试创建一个显示多页图像并允许用户缩放、旋转和浏览图像的 UserControl。我遇到的一个问题是正确设置键盘快捷键。我发现我需要在托管 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");
        }        
    }
}

【问题讨论】:

    标签: wpf user-controls commandbinding inputbinding


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 在代码中创建命令是否比在 XAML 中创建命令更好?这是否也意味着您不能在 XAML 中将 InputBinding 添加到 UserControl 的主机?此外,此解决方案不起作用。 UserControl1 没有 KeyboardFocus,因此 InputBinding 永远不会触发。如果您单击按钮然后使用 KeyGesture 将显示 MessageBox
    • 您是否使用代码或 XAML 来创建命令实际上取决于您 - 两者都不是“更好”。我在这里选择代码是因为它需要的行数更少,因此可能更容易理解。我已经编辑了答案以将命令绑定到窗口。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2015-05-21
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2013-12-13
    相关资源
    最近更新 更多