# 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**

```C#
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**

```C#
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:

1. Create your own class that inherits from `DataFormCommandProvider`:

#### **Define a Custom CommandProvider**

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

1. Override the methods of the commands that will be customized.

The following example aims at changing the `MoveCurrentToNext` and `MoveCurrentToPrevious` execution 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**

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

A common requirement for the DataForm is to add confirmation message boxes when CRUD operations are executed. The example below implements the scenario for the `CommitEdit` and `CancelEdit` commands.

#### **Customizing CommitEdit and CancelEdit**

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

1. The last thing to do is set the `CommandProvider` property of the DataForm to the newly-created `CustomKeyboardCommandProvider` class:

#### **Set the CommandProvider Property in XAML**

```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**

```C#
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**

```C#
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

* [Getting Started with the Telerik UI for WinUI DataForm](https://www.telerik.com/winui/documentation/controls/raddataform/getting-started)
* [CRUD Operations Support](https://www.telerik.com/winui/documentation/controls/raddataform/crud-operations)
