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

Commands

RadCardView provides a set of built-in commands that enables you to easily handle the user interface actions, but still make your logic independent of the UI layout. Thus, you are not obliged to subscribe to a particular event in order to achieve the desired behavior.

All supported commands are defined in the static RadCardViewCommands class and are listed below:

  • BeginEdit—Invokes editing of the current item. Press the F2 or Enter keys to invoke it via the keyboard.
  • CommitEdit—Forces all the changes to be committed. If the RadCardView is currently in edit mode, pressing the Enter key will invoke this command.
  • CancelEdit—Causes the editing of the item to be discontinued. If the RadCardView is currently in edit mode, pressing the Escape key will invoke this command.
  • MoveUp, MoveDown—Moves the selection to the item situated above/below the selected one. You can also invoke these commands by pressing the Up or Down arrow keys.
  • MoveLeft, MoveRight—Moves the selection to the item situated on the left/right side of the selected one. You can also invoke these commands by pressing the Left or Right arrow keys.
  • MoveFirst, MoveLast—Moves the selection to the first/last item on the current row/column (depending on the CardLayout property). Executed when pressing the Home or End keys.
  • MoveNext, MovePrevious—Moves the selection to the next/previous item with regards to its position in the collection. The MoveNext command is also invoked when pressing the Tab key. The MovePrevious is invoked when holding the Shift key and pressing the Tab one.
  • MoveHome, MoveEnd—Moves the selection to the first/last item in the collection. Executed when holding the Control key and pressing the Home or End keys.
  • MovePageUp, MovePageDown—Moves the focus to the item situated one viewport away from the current item. Executed when pressing the PageUp or PageDown keys.
  • MoveTop, MoveBottom—Moves the selection to the item situated in the first/last item. Executed when holding the Control key and pressing the PageUp or PageDown keys.
  • SelectCurrentItem—Selects the current item.
  • ExpandCardViewItem, CollapseCardViewItem—Expands/collapses the selected item. Takes as a parameter the item to be expanded/collapsed. If no parameter is provided, the SelectedItem is expanded/collapsed.

As the commands provided by RadCardView are ICommands at their core, they do provide methods for both checking if they can be invoked - CanExecute and for invoking them - Execute.

Using the RadCardViewCommands 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 the selection down and then collapse it. However, when invoking the commands in such a manner a second parameter should be added, pointing out the target UI Element as shown in the following example:

Executing different commands

private void Button1_Click(object sender, RoutedEventArgs e) 
{ 
    var moveDownCommand = RadCardViewCommands.MoveDown as RoutedUICommand; 
    var collapseCommand = RadCardViewCommands.CollapseCardViewItem as RoutedUICommand; 
    moveDownCommand.Execute(null, this.cardView); 
    collapseCommand.Execute(null, this.cardView); 
} 
In order to ensure that all commands will be executed in the correct sequence, it is advised to use RadCardView's PendingCommands collection as demonstrated below:

Executing different commands with the ExecutePendingCommand method

private void Button2_Click(object sender, RoutedEventArgs e) 
{ 
    this.cardView.PendingCommands.Add(RadCardViewCommands.MoveDown); 
    this.cardView.PendingCommands.Add(RadCardViewCommands.CollapseCardViewItem); 
    this.cardView.ExecutePendingCommand(); 
} 

Keyboard Command Provider

RadCardView provides a set of keyboard navigation scenarios that will result in certain consequence of commands to be executed. However, in case you require them not to be invoked at all or to utilize different commands, you may take advantage of the IKeyboardCommandProvider interface and implement your custom logic. Thus, you will be able to change the behavior of all keys. The hurdle here is the requirement for predefining each one of the commands.

Another approach for accomplishing the purpose will be to create a separate class, inheriting the DefaultKeyboardCommandProvider and overriding the ProvideCommandsForKey method. In this way only the undesired behavior can be adjusted according to your requirements.

The custom class responsible for the update of the commands needs to be similar to the one below:

Defining the custom IKeyboardCommandProvider

public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider 
{ 
    private RadCardView parentCardView; 
 
    public CustomKeyboardCommandProvider(RadCardView cardView) 
     : base(cardView) 
    { 
        this.parentCardView = cardView; 
    } 
 
    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key) 
    { 
        List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList(); 
 
        if (key == Key.Enter) 
        { 
            commandsToExecute.Clear(); 
            commandsToExecute.Add(RadCardViewCommands.CommitEdit); 
            commandsToExecute.Add(RadCardViewCommands.MoveNext); 
            commandsToExecute.Add(RadCardViewCommands.BeginEdit); 
        } 
 
        return commandsToExecute; 
    } 
} 
Following up the code-snippet above, a press of Enter key will result in saving the current changes, moving the focus to the next item and editing it. However, do not forget to remove the predefined commands for that particular key by calling the Clear method.

The last thing to be done is to set KeyboardCommandProvider property of the RadCardView control to be the newly-created CustomKeyboardCommandProvider class:

Setting the KeyboardCommandProvider property

this.cardView.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.cardView); 

See Also

In this article