Commands Overview
The DataGrid provides an intuitive set of APIs that allow different aspects of its behavior to be handled and/or overridden. To control the behavior of the component, you can add commands through the Commands
property, which gets a collection with all of the custom commands registered with the CommandService
property.
CommmandService
The DataGrid exposes a CommandService
property which provides the following methods:
CanExecuteCommand(CommandId id, object parameter)
—Determines whether the command, associated with the specifiedId
, can be executed based on the provided parameter. If a custom command with the providedId
is added to theCommands
collection, it will take precedence over the default (built-in) one.CanExecuteDefaultCommand(CommandId id, object parameter)
—Determines whether the default command, associated with the specifiedId
, can be executed based on the provided parameter.ExecuteCommand(CommandId id, object parameter)
—Attempts to find the command, associated with the specifiedId
, and to perform itsExecute
routine by using the provided parameter. If a custom command with the providedId
is added to theCommands
collection, it will take precedence over the default one.ExecuteDefaultCommand(CommandId id, object parameter)
—Executes the default command (without looking for user-defined commands) that is associated with the specifiedId
.
Command Types
The DataGrid supports the following types of commands:
-
DataGridCommand
—All the default commands within the DataGrid derive from the baseDataGridCommand
. Think of this command as a UI-related one as it operates over the DataGrid instance that owns the command.Custom commands take precedence over the built-in (default) ones.
-
DataGridUserCommand
—This specific command type provides an abstraction for the UI-related commands over a stateless and UI-agnostic generic command implementation.DataGridUserCommand
exposes the following properties:-
Id
—Gets or sets theCommandId
member with which this command is associated. -
Command
—Gets or sets the genericICommand
implementation that may come from theViewModel
. -
EnableDefaultCommand
—Gets or sets a value indicating whether the default UI command associated with the specifiedId
will be executed. The default value isTrue
.
-
Bind to a Command in the ViewModel
<Grid xmlns:grid="using:Telerik.UI.Xaml.Controls.Grid"
xmlns:gridCommands="using:Telerik.UI.Xaml.Controls.Grid.Commands">
<grid:RadDataGrid Width="600" Height="460" x:Name="grid">
<grid:RadDataGrid.Commands>
<gridCommands:DataGridUserCommand Id="CellTap" Command="{Binding MyCommand}"/>
</grid:RadDataGrid.Commands>
</grid:RadDataGrid>
</Grid>
CommandId Enumeration
All the predefined commands are identified by a member of the CommandId
enumeration. This is actually the key that relates a command instance to a particular action or routine within the owning grid. To register a custom command, either inherit the DataGridCommand
class and override its CanExecute
and Execute
methods, or instantiate a DataGridUserCommand
instance and set its Command
property. In both cases you need to set the Id
property of the new command so that it can be properly associated with the desired action or event.
CommandId
supports the following enumerations: