New to Telerik UI for WinUI? Download free 30-day trial

Commands Support

This article will demonstrate how to customize RadDataForm's commands in an MVVM-friendly way as well as how to execute them manually.

DataFormCommandProvider

The public DataFormCommandProvider class exposes the "execute" and "can-execute" logic of all commands utilized by RadDataForm. RadDataForm processes these commands according to its current provider.

Execute Logic Methods

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

Example 1: The Execute Methods

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

Can-Execute Logic Methods

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

Example 2: The CanExecute Methods

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

Designing a Custom DataFormCommandProvider

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

Example 3: Defining a Custom CommandProvider

public class CustomCommandProvider : DataFormCommandProvider 
{ 
    public CustomCommandProvider() : base(null) 
    { 
    } 
    public CustomCommandProvider(RadDataForm dataForm) 
            : base(dataForm) 
    { 
        this.DataForm = dataForm; 
    } 
    //. . . 
} 
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:

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(); 
    } 
} 

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.

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(); 
        } 
    } 
} 

Setting the Custom DataFormCommandProvider

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

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}"/> 

Example 6: Set the CommandProvider Property

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

Executing Commands Manually

Using the RadDataFormCommands class, you can set a sequence of commands to be performed one after another. So, for example, you may easily handle the click event of a button, move to the next item and put it in edit mode. However, when invoking the commands in such a manner a second parameter should be added, pointing out the target UI Element as shown in Exapmle 7.

Example 7: Executing a sequence of commands

private void Button1_Click(object sender, RoutedEventArgs e) 
{ 
    var moveToNextCommand = RadDataFormCommands.MoveCurrentToNext as RoutedUICommand; 
    var editCommand = RadDataFormCommands.BeginEdit as RoutedUICommand; 
    moveToNextCommand.Execute(null, this.DataForm); 
    editCommand.Execute(null, this.DataForm); 
} 

See Also

In this article
Not finding the help you need?