Commands
The command design-pattern is very important and widely used in the XAML and MVVM world. RadListView follows these best practices and provides an intuitive and easy-to-use set of APIs that allow different aspects of the RadListView control’s behavior to be handled and/or completely overridden.
RadListView exposes a commands collection that allows you to register custom commands with each control’s instance through the RadListView.Commands property. To register a command, use the CommandService class. Note that custom commands have higher priority than the built-in (default) ones.
Command Types
There are two types of commands:
- ListViewCommand: All the default commands within RadListView derive from the base ListViewCommand. Think of this command as a UI-related command as it operates over the RadListView instance that owns the command.
-
ListViewUserCommand: This specific command type provides the abstraction of the UI-related commands (that all the built-in commands are) over a stateless and UI-agnostic generic command implementation. The command exposes the following properties:
- Id: Gets or sets the CommandId member this command is associated with.
- Command: Gets or sets the generic ICommand implementation that may come from the ViewModel.
- EnableDefaultCommand: Gets or sets a value indicating whether the default (built-in) UI command associated with the specified Id will be executed. The default value is True.
CommandId Enumeration
All the predefined commands within a RadListView instance are identified by a member of the CommandId enumeration. This is actually the key that relates a command instance to a particular action/routine within the owning list view. In order to register a custom command within a RadListView instance you may either inherit the ListViewCommand class and override its CanExecute and Execute methods or instantiate a ListViewUserCommand instance and set its Command property. In both cases you need to set the Id property of the new command so that it can be properly associated with the desired action/event. Following are the members of the CommandId enumerations:
- LoadMoreData: A command associated with requesting and loading more data when available.
- ItemTap: A command associated with the Tap event that occurs over an item.
- ItemDragStarting: A command associated with the starting a drag action on an item.
- ItemReorderComplete: A command associated with the completion of a item reorder action on an item.
- ItemSwiping: A command associated with the swiping action on an item.
- ItemSwipeActionComplete: A command associated with the completion of a item swipe action on an item.
- ItemActionTap: A command associated with the Tap event that occurs on an action item.
- RefreshRequested: A command associated with requesting an update with the pull to refresh control.
Examples
Using the ListViewCommand class
This example demonstrates how to use the ListViewCommand class to create a command that overrides the default one.
Example 1: Creating a custom command
Example 2: Using the custom command
Using the ListViewUserCommand class
The following example demonstrates how to create ListViewUserCommand which is bound to a command from a view model. We will use the item tap command and execute our custom action instead of the default one.
First, we have to define a view model as a static resource.
Example 3: Defining the view model in Xaml
Here is the definition of the RadListView in XAML.
Example 4: Adding the command in Xaml
Where the telerikData
and telerikListViewCommands
points to: