Edit this page

Customizing Commands

This article provides a description and examples of how to customize RadDataForm’s commands in MVVM-friendly way.

Please note that this functionality is introduced with Q1 2013.

Telerik.Windows.Controls.Data.DataForm.DataFormCommandProvider is a public class that exposes the “execute” and “can-execute” logic of all commands, utilized by RadDataForm. RadDataForm processes these commands according to its current provider. Here is the set of methods that DataFormCommandProvider exposes:

Execute Logic Methods

The methods listed below identify the logic that is executed when a certain command’s invocation takes place.

[C#] Example 1: The IExecuteLogicMethods

void MoveCurrentToFirst();
void MoveCurrentToLast();
void MoveCurrentToNext();
void MoveCurrentToPrevious();
void BeginEdit();
void CommitEdit();
void Delete();
void CancelEdit();
void AddNew();

[VB.NET] Example 1: The IExecuteLogicMethods

Sub MoveCurrentToFirst()
Sub MoveCurrentToLast()
Sub MoveCurrentToNext()
Sub MoveCurrentToPrevious()
Sub BeginEdit()
Sub CommitEdit()
Sub Delete()
Sub CancelEdit()
Sub AddNew()

Can-Execute Logic Methods

With the help of those methods you can identify whether a certain command can be executed or not.

[C#] Example 2: The ICanExecuteLogicMethods

{
    bool CanMoveCurrentToFirstExecute();
    bool CanMoveCurrentToLastExecute();
    bool CanMoveCurrentToNextExecute();
    bool CanMoveCurrentToPreviousExecute();
    bool CanBeginEditExecute();
    bool CanCommitEditExecute();
    bool CanDeleteExecute();
    bool CanCancelEditExecute();
    bool CanAddNewExecute();

[VB.NET] Example 2: The ICanExecuteLogicMethods

Function CanMoveCurrentToFirstExecute() As Boolean
Function CanMoveCurrentToLastExecute() As Boolean
Function CanMoveCurrentToNextExecute() As Boolean
Function CanMoveCurrentToPreviousExecute() As Boolean
Function CanBeginEditExecute() As Boolean
Function CanCommitEditExecute() As Boolean
Function CanDeleteExecute() As Boolean
Function CanCancelEditExecute() As Boolean
Function CanAddNewExecute() As Boolean

A runnable demo that utilizes the code from this article can be found on the DataForm's CustomCommandsProvider demo.

Designing a custom CommandProvider

The first step is to create your own class that inherits from DataFormCommandProvider:

[C#] Example 3: Defining a Custom CommandProvider

public class CustomCommandProvider : DataFormCommandProvider
{
    public CustomCommandProvider() : base(null)
    {
    }
    public CustomCommandProvider(RadDataForm dataForm)
            : base(dataForm)
    {
        this.DataForm = dataForm;
    }
    //. . .
}

[VB.NET] Example 3: Defining a Custom CommandProvider

Public Class CustomCommandProvider
    Inherits DataFormCommandProvider
    Public Sub New()
        MyBase.New(Nothing)
    End Sub
    Public Sub New(dataForm As RadDataForm)
        MyBase.New(dataForm)
        Me.DataForm = dataForm
    End Sub
    '. . .
End Class

You need to override the methods of the commands that will be customized. In the following example we will customize: MoveCurrentToNext, MoveCurrentToPrevious, BeginEdit and CancelEdit.

MoveCurrentToNext and MoveCurrentToPrevious

We aim at changing their execution logic, in order to start editing the item, as soon as it was set as a current one and bypass the logic in the same place. Here are the overridden methods:

[C#] Example 4: Customizing MoveCurrentToNext and MoveCurrentToPrevious

protected override void MoveCurrentToNext()
{
    if (this.DataForm != null)
    {
        this.DataForm.MoveCurrentToNext();
        this.DataForm.BeginEdit();
    }
}
protected override void MoveCurrentToPrevious()
{
    if (this.DataForm != null)
    {
        this.DataForm.MoveCurrentToPrevious();
        this.DataForm.BeginEdit();
    }
}

[VB.NET] Example 4: Customizing MoveCurrentToNext and MoveCurrentToPrevious

Protected Overrides Sub MoveCurrentToNext()
    If Me.DataForm IsNot Nothing Then
        Me.DataForm.MoveCurrentToNext()
        Me.DataForm.BeginEdit()
    End If
End Sub
Protected Overrides Sub MoveCurrentToPrevious()
    If Me.DataForm IsNot Nothing Then
        Me.DataForm.MoveCurrentToPrevious()
        Me.DataForm.BeginEdit()
    End If
End Sub

CommitEdit and CancelEdit

A common requirement for data form is to add confirmation message boxes when CRUD operations are executed. Here we will do a similar thing with the CommitEdit/CancelEdit commands.

[C#] Example 5: Customizing CommitEdit and CancelEdit

protected override void CommitEdit()
{
    MessageBoxResult result = MessageBox.Show("Commit changes for the current edit item?", "CommitEdit confirmation", MessageBoxButton.OKCancel);
    if (result == MessageBoxResult.OK)
    {
        if (this.DataForm != null && this.DataForm.ValidateItem())
        {
            this.DataForm.CommitEdit();
        }
    }
}
protected override void CancelEdit()
{
    MessageBoxResult result = MessageBox.Show("Cancel changes for the current edit item?", "CancelEdit confirmation", MessageBoxButton.OKCancel);
    if (result == MessageBoxResult.OK)
    {
        if (this.DataForm != null)
        {
            this.DataForm.CancelEdit();
        }
    }
}

[VB.NET] Example 5: Customizing CommitEdit and CancelEdit

Protected Overrides Sub CommitEdit()
    Dim result As MessageBoxResult = MessageBox.Show("Commit changes for the current edit item?", "CommitEdit confirmation", MessageBoxButton.OKCancel)
    If result = MessageBoxResult.OK Then
        If Me.DataForm IsNot Nothing AndAlso Me.DataForm.ValidateItem() Then
            Me.DataForm.CommitEdit()
        End If
    End If
End Sub
Protected Overrides Sub CancelEdit()
    Dim result As MessageBoxResult = MessageBox.Show("Cancel changes for the current edit item?", "CancelEdit confirmation", MessageBoxButton.OKCancel)
    If result = MessageBoxResult.OK Then
        If Me.DataForm IsNot Nothing Then
            Me.DataForm.CancelEdit()
        End If
    End If
End Sub

The last thing to be done is to set CommandProvider Property of the RadDataForm to be the newly-created CustomKeyboardCommandProvider class:

[XAML] Example 6: Set the CommandProvider Property

<Grid.Resources>
    <my:CustomCommandProvider x:Key="CustomProvider"/>
</Grid.Resources>
<telerik:RadDataForm x:Name="RadDataForm1"
             ItemsSource="{Binding Employees}" 
             CommandProvider="{StaticResource CustomProvider}"/>

[C#] Example 6: Set the CommandProvider Property

this.RadDataForm1.CommandProvider = new CustomCommandProvider(this.RadDataForm1);

[VB.NET] Example 6: Set the CommandProvider Property

Me.RadDataForm1.CommandProvider = New CustomCommandProvider(Me.RadDataForm1)