0

我有一个场景,在我的自定义控件中,我有一个名为MinValuetype的自定义依赖属性int

我想允许用户通过键盘输入 Ctrl++(增加)和 Ctrl+-(减少)来更改该值。我知道这可以通过命令来完成,但是对于在哪里实现这些命令一无所知。无论窗口中的哪个控件具有焦点,用户都应该能够使用前面提到的键盘快捷键。

我想我必须在我的自定义控件中实现命令,因为只有那个控件知道如何处理键盘快捷键,但是因为我有一个充满自定义控件的列表框,每个都应该处理快捷键,我不知道如何那行得通。任何指针?一如既往的感谢!

4

1 回答 1

0

还有其他方法可以做到这一点,并且可能有充分的理由不这样做,但这里有一个将命令放在用户控件上的实现:

用户控件代码(可以很容易地成为一个无外观的控件):

public partial class TestControl : UserControl
    {
        //Declare routed commands
        public static RoutedCommand IncrementCommand;
        public static RoutedCommand DecrementCommand;

        static TestControl()
        {
            //Create routed commands and add key gestures.
            IncrementCommand = new RoutedCommand();
            DecrementCommand = new RoutedCommand();
            IncrementCommand.InputGestures.Add(new KeyGesture(Key.Add, ModifierKeys.Control));
            DecrementCommand.InputGestures.Add(new KeyGesture(Key.Subtract, ModifierKeys.Control));
        }

        public TestControl()
        {
            //Subscribe to Increment/Decrement events.
            TestControl.Decremented += down;
            TestControl.Incremented += up;

            InitializeComponent();
        }

        //Declare *static* Increment/Decrement events.
        public static event EventHandler Incremented;
        public static event EventHandler Decremented;

        //Raises Increment event
        public static void IncrementMin(object o, ExecutedRoutedEventArgs args)
        {
            if (Incremented != null)
            {
                Incremented(o, args);
            }
        }

        //Raises Decrement event
        public static void DecrementMin(object o, ExecutedRoutedEventArgs args)
        {
            if (Decremented != null)
            {
                Decremented(o, args);
            }
        }

        //Handler for static increment
        private void down(object o, EventArgs args)
        {
            Min--;
        }

        //Handler for static decrement
        private void up(object o, EventArgs args)
        {
            Min++;
        }

        //Backing property
        public int Min
        {
            get { return (int)GetValue(MinProperty); }
            set { SetValue(MinProperty, value); }
        }

        public static readonly DependencyProperty MinProperty =
            DependencyProperty.Register("Min", typeof(int),
            typeof(TestControl), new UIPropertyMetadata(0));
    }

需要此行为的窗口需要为这些命令添加 CommandBinding。后面的窗口代码:

public MainWindow()
{
    InitializeComponent();

    //Raise control's static events in response to routed commands
    this.CommandBindings.Add(
        new CommandBinding(TestControl.IncrementCommand, new ExecutedRoutedEventHandler(TestControl.IncrementMin)));
    this.CommandBindings.Add(
        new CommandBinding(TestControl.DecrementCommand, new ExecutedRoutedEventHandler(TestControl.DecrementMin)));            
}

这有帮助吗?

(哦,顺便说一句 - 正如所写的那样,这段代码只响应我键盘上的 +/- 键,而不是 (P)([)(]) 键上方的那些 - 我以前没有使用过按键手势。当然它可以不必纠正。)

于 2013-11-10T00:53:50.277 回答