Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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:</p> <p>User control code (could just as easily be a lookless control):</p> <pre><code>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)); } </code></pre> <p>The windows on which this behavior is desired need to add a CommandBinding for these commands. Window code behind:</p> <pre><code>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))); } </code></pre> <p>Does that help?</p> <p>(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.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload