Category / Section
How to map the custom commands to existing gestures (keyboard shortcuts and mouse) in WPF Diagram (SfDiagram)?
1 min read
WPF Diagram (SfDiagram) provides support to map or bind command execution with desired combination of key gestures and some built-in commands. The CommandManager provides support to define custom commands. The custom commands are executed, when the specified key gesture is recognized.
To define a custom command, specify the following properties.
- Gesture: Class represents the combination of keys, key modifiers, and key states.
- Command: Property for custom command.
- Parameter: Property for additional information for the command execution. This is not a mandatory to set.
Refer to the following code example and sample for creating the custom command to save the diagram.
ObservableCollection<IGestureCommand> commands= diagram.CommandManager.Commands; //DataContext of the window or diagram CommandViewModel viewModel = (this.DataContext as CommandViewModel); //Create GestureCommand with gesture keys and commands. GestureCommand command = new GestureCommand(); Gesture gesture = new Gesture() { Key = Key.S, KeyModifiers = ModifierKeys.Control, KeyState = KeyStates.Down }; command.Gesture = gesture; //Set command from viewmodel to diagram command.Command =viewModel==null? viewModel.Save:null; command.Parameter = this.diagram; commands.Add(command);