Commands
The DataForm enables you to control its behavior by customizing its available commands in an MVVM-friendly way.
DataFormCommandProvider
The DataForm provides the public DataFormCommandProvider class, which exposes the "execute" and "can-execute" logic for all its commands. The component processes these commands according to its current provider.
Execute Logic Methods
The "execute" logic methods identify the logic that is executed when a certain command is invoked.
Execute Logic Methods
void MoveCurrentToFirst();
void MoveCurrentToLast();
void MoveCurrentToNext();
void MoveCurrentToPrevious();
void BeginEdit();
void CommitEdit();
void Delete();
void CancelEdit();
void AddNew();
Can-Execute Logic Methods
The "can-execute" logic methods enable you to identify whether a certain command can be executed or not.
CanExecute Logic Methods
bool CanMoveCurrentToFirstExecute();
bool CanMoveCurrentToLastExecute();
bool CanMoveCurrentToNextExecute();
bool CanMoveCurrentToPreviousExecute();
bool CanBeginEditExecute();
bool CanCommitEditExecute();
bool CanDeleteExecute();
bool CanCancelEditExecute();
bool CanAddNewExecute();
Customizing the DataFormCommandProvider
To design and set a custom DataFormCommandProvider, follow the steps:
-
Create your own class that inherits from
DataFormCommandProvider:Define a Custom CommandProvider
public class CustomCommandProvider : DataFormCommandProvider { public CustomCommandProvider() : base(null) { } public CustomCommandProvider(RadDataForm dataForm) : base(dataForm) { this.DataForm = dataForm; } //. . . } -
Override the methods of the commands that will be customized.
The following example aims at changing the
MoveCurrentToNextandMoveCurrentToPreviousexecution logic to start editing the item immediately after it is set as a current one and bypass the logic in the same place.Customizing MoveCurrentToNext and MoveCurrentToPrevious
A common requirement for the DataForm is to add confirmation message boxes when CRUD operations are executed. The example below implements the scenario for theprotected 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(); } }CommitEditandCancelEditcommands.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(); } } } -
The last thing to do is set the
CommandProviderproperty of the DataForm to the newly-createdCustomKeyboardCommandProviderclass:Set the CommandProvider Property in XAML
<Grid.Resources> <my:CustomCommandProvider x:Key="CustomProvider"/> </Grid.Resources> <telerik:RadDataForm x:Name="RadDataForm1" ItemsSource="{Binding Employees}" CommandProvider="{StaticResource CustomProvider}"/>Set the CommandProvider Property in Code
this.RadDataForm1.CommandProvider = new CustomCommandProvider(this.RadDataForm1);Executing Commands Manually
By using the RadDataFormCommands class, you can set a sequence of commands that will be performed one after another. For example, you can handle the click event of a button, move to the next item, and place the DataForm it in edit mode. However, when invoking the commands in such a manner, you have to add a second parameter that points out the target UI element, as shown in the following example.
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);
}