开发者

Custom Command in WPF to allow dependency property value change in "realtime"

开发者 https://www.devze.com 2022-12-31 05:48 出处:网络
I have a scenario where in my custom control I have a custom Dependency Property called MinValue of type int.

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 开发者_StackOverflowonly 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!


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消