WPF中的自定义命令允许“实时”中的依赖项属性值更改

时间:2010-05-18 12:52:44

标签: c# .net wpf commandbinding

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

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

我想我必须在我的自定义控件中实现Command,因为只有那个控件知道如何处理键盘快捷键,但由于我有一个完整的自定义控件的列表框,并且每个都应该处理快捷方式,我是不知道那会怎么样。 有什么指针吗? 一如既往地谢谢!

1 个答案:

答案 0 :(得分: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)([)(])键上面的键 - 我还没有使用关键手势以前。肯定不能纠正。)