Commands
The Buttons implement the ICommandSource interface which provides a command support where any button can be bound to an ICommand object that will be executed on click.
The ICommandSource interface adds the following properties to the RadButton classes:
Command—TheICommandobject that will be executed on button click.CommandParameter—Theobjectthat will be passed as the parameter of theExecutemethod of the command.CommandTarget—TheUIElementon which the command is being executed.
The DelegateCommand class is a relay command implementation that can be easily used in your view models. It implements the
ICommandinterface and allows you to provide two actions -ExecuteandCanExecute.
The following example demonstrates how to data bind the Command property of RadButton to an ICommand property of the view model. The same approach can be used also with the other buttons from the suite - like RadToggleButton, RadRadioButton, etc.
-
Create a view model class and define the command property.
Defining the view model
public class SampleViewModel { public ICommand MyCommand { get; set; } public SampleViewModel() { MyCommand = new DelegateCommand(OnCommandExecuted); } private void OnCommandExecuted(object obj) { string parameter = (string)obj; // where parameter will be "ParameterValue" coming from the CommandParameter set on the button } } -
Assing the view model to the DataContext property of the button or the view that hosts it.
Setting up the DataContext
public UserControl() { InitializeComponent(); this.button.DataContext = new SampleViewModel(); } -
Set command properties in XAML.
Setting up the Command and CommandParameter properties
<telerik:RadButton x:Name="button" Content="My Button" Command="{Binding MyCommand}" CommandParameter="ParameterValue" />See Also
- Getting Started
- Events