Class CommandManager
Represents the manager that handles a collection of custom commands and their associated key gestures in a diagram component.
Inheritance
Namespace: Syncfusion.Blazor.Diagram
Assembly: Syncfusion.Blazor.dll
Syntax
public class CommandManager : ComponentBase
Remarks
The CommandManager class facilitates the definition and execution of custom commands in response to particular key gestures. This is especially useful for implementing keyboard shortcuts to enhance user interaction with diagram components.
Custom commands must be registered with their specific KeyGesture and should adhere to a consistent naming convention to avoid command collisions.
Examples
The following code example demonstrates how to set up a diagram with custom commands for grouping and ungrouping nodes using specific keyboard shortcuts.
<SfDiagramComponent @ref="@diagram" Height="600px" Nodes="@nodes">
@* Initializing the custom commands *@
<CommandManager Commands="@command" Execute="@CommandExecute" CanExecute="@CanExe">
</CommandManager>
</SfDiagramComponent>
@code {
// Reference to the diagram component
SfDiagramComponent diagram;
// Defining custom keyboard commands
DiagramObjectCollection<KeyboardCommand> command = new DiagramObjectCollection<KeyboardCommand>()
{
new Command()
{
Name = "CustomGroup",
Gesture = new KeyGesture() { Key = DiagramKeys.G, KeyModifiers = KeyModifiers.Control }
},
new Command()
{
Name = "CustomUngroup",
Gesture = new KeyGesture() { Key = DiagramKeys.U, KeyModifiers = KeyModifiers.Control }
},
};
// Define the nodes collection for the diagram
DiagramObjectCollection<Node> nodes = new DiagramObjectCollection<Node>();
// Determines if the command can be executed
public void CanExe(CommandKeyArgs args)
{
args.CanExecute = true;
}
// Executes the custom command based on the gesture
public void CommandExecute(CommandKeyArgs args)
{
if (args.Gesture.KeyModifiers == KeyModifiers.Control && args.Gesture.Key == DiagramKeys.G)
{
// Custom command to group the selected nodes
diagram.Group();
}
else if (args.Gesture.KeyModifiers == KeyModifiers.Control && args.Gesture.Key == DiagramKeys.U)
{
Selector selector = diagram.SelectedItems;
// Custom command to ungroup the selected items
if (selector.Nodes.Count > 0 && selector.Nodes[0] is Group)
{
if ((selector.Nodes[0] as Group).Children.Length > 0)
{
diagram.Ungroup();
}
}
}
}
}
Constructors
CommandManager()
Declaration
public CommandManager()
Properties
CanExecute
Gets or sets the callback mechanism to determine whether a command can be executed in its current state.
Declaration
public EventCallback<CommandKeyArgs> CanExecute { get; set; }
Property Value
Type | Description |
---|---|
Microsoft.AspNetCore.Components.EventCallback<CommandKeyArgs> | An Microsoft.AspNetCore.Components.EventCallback<> that signifies the event fired to check command execution eligibility. The default value is null. |
Remarks
This property is used to validate if the command can proceed based on specific conditions.
Make sure to set the appropriate logic within the execution of the provided callback.
Examples
The following example shows how to implement a simple command that can always execute:
<SfDiagramComponent>
@* Initializing the custom commands *@
<CommandManager CanExecute="@CanExe">
</CommandManager>
</SfDiagramComponent>
@code
{
public void CanExe(CommandKeyArgs args)
{
args.CanExecute = true;
}
}
ChildContent
Gets or sets the child content of the CommandManager.
Declaration
public RenderFragment ChildContent { get; set; }
Property Value
Type | Description |
---|---|
Microsoft.AspNetCore.Components.RenderFragment | A Microsoft.AspNetCore.Components.RenderFragment representing the child content within the CommandManager. The default value is null. |
Remarks
The child content encapsulates any rendering logic or elements that should be displayed as part of the command manager's visual tree.
Commands
Gets or sets the commands that are mapped within the CommandManager.
Declaration
public DiagramObjectCollection<KeyboardCommand> Commands { get; set; }
Property Value
Type | Description |
---|---|
DiagramObjectCollection<KeyboardCommand> | A DiagramObjectCollection<T> containing the commands. The default value is null. |
Remarks
This property holds the collection of keyboard commands that can be executed within the command manager.
Each command maps a KeyboardCommand, which includes a command name and its associated key gesture.
Examples
The following code example demonstrates how to define custom commands within a command manager.
<SfDiagramComponent>
<CommandManager Commands="@command" >
</CommandManager>
</SfDiagramComponent>
@code
{
DiagramObjectCollection<KeyboardCommand> command = new DiagramObjectCollection<KeyboardCommand>()
{
new Command()
{
Name = "CustomGroup",
Gesture = new KeyGesture() { Key = DiagramKeys.G, Modifiers = ModifierKeys.Control }
},
new Command()
{
Name = "CustomUngroup",
Gesture = new KeyGesture() { Key = DiagramKeys.U, Modifiers = ModifierKeys.Control }
},
};
}
CommandsChanged
Gets or sets the callback to trigger when the command collection changes within the CommandManager.
Declaration
public EventCallback<DiagramObjectCollection<KeyboardCommand>> CommandsChanged { get; set; }
Property Value
Type | Description |
---|---|
Microsoft.AspNetCore.Components.EventCallback<DiagramObjectCollection<KeyboardCommand>> | An EventCallback with a DiagramObjectCollection of KeyboardCommand that contains the event data. |
Remarks
Attach a method to this event to receive notifications whenever the collection of commands is modified.
Execute
Gets or sets the asynchronous callback mechanism that triggers when a command is executed in the diagram component.
Declaration
public EventCallback<CommandKeyArgs> Execute { get; set; }
Property Value
Type | Description |
---|---|
Microsoft.AspNetCore.Components.EventCallback<CommandKeyArgs> | An Microsoft.AspNetCore.Components.EventCallback<> representing the command execution event. The default value is null. |
Remarks
This property enables the customization of keyboard interactions with the diagram, allowing commands to be executed based on specified key gestures.
Define the logic for custom command execution, such as grouping or ungrouping diagram components, by implementing this event handler.
Examples
The following example demonstrates how to create custom keyboard commands:
<SfDiagramComponent>
@* Initializing the custom commands *@
<CommandManager Execute="@CommandExecute">
</CommandManager>
</SfDiagramComponent>
@code
{
public void CommandExecute(CommandKeyArgs args)
{
if (args.Gesture.Modifiers == ModifierKeys.Control && args.Gesture.Key == DiagramKeys.G)
{
// Custom command to group the selected nodes
diagram.Group();
}
if (args.Gesture.Modifiers == ModifierKeys.Control && args.Gesture.Key == DiagramKeys.U)
{
Selector selector = diagram.SelectedItems;
// Custom command to ungroup the selected items
if (selector.Nodes.Count > 0 && selector.Nodes[0] is Group)
{
if ((selector.Nodes[0] as Group).Children.Length > 0)
{
diagram.Ungroup();
}
}
}
}
}
Methods
BuildRenderTree(RenderTreeBuilder)
Declaration
protected override void BuildRenderTree(RenderTreeBuilder __builder)
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder | __builder |
Dispose()
This method releasing all unmanaged resources.
Declaration
public void Dispose()
OnInitializedAsync()
Method invoked when the component is ready to start.
Declaration
protected override Task OnInitializedAsync()
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task | A System.Threading.Tasks.Task representing any asynchronous operation. |