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

发布于 2024-09-01 14:47:38 字数 326 浏览 2 评论 0原文

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

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

我认为我必须在自定义控件中实现该命令,因为只有该控件知道如何处理键盘快捷键,但由于我有一个充满自定义控件的列表框,并且每个控件都应该处理快捷键,所以我不确定它是如何工作的。 有什么指点吗? 一如既往的感谢!

I have a scenario where in my custom control I have a custom Dependency Property called MinValue of type int.

I want to allow the user to change that value via keyboard input Ctrl++ (increase) and Ctrl+- (decrease). I undertand this can be done with Commands, but am clueless as to where to implement those Commands.
The user should be able to use the beforementioned keyboard shortcuts no matter what control in the Window has focus.

I think I would have to implement the Command in my custom control because only that control knows what to do with the keyboard shortcut, but since I have a listbox full of custom controls, and each should handle the shortcut, I'm not sure how that would work.
Any pointers?
Thanks as always!

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

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

发布评论

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

评论(1

咽泪装欢 2024-09-08 14:47:38

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

控件代码(可能很容易成为无外观的控件):

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)([)(]) 键上方的键 - 我以前没有使用过按键手势。当然可以不必太纠正。)

There are other ways to do this and probably good reasons to not do it this way, but here's an implementation that puts the commands on the user control:

User control code (could just as easily be a lookless control):

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

The windows on which this behavior is desired need to add a CommandBinding for these commands.
Window code behind:

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

Does that help?

(Oh, BTW - as written this code only responds to the +/- keys on my keypad, not the ones above the (P)([)(]) keys - I haven't worked with key gestures before. Surely it can't be very had to correct.)

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