Commands
The RadButtons provide a standard ICommandSource implementation and allow you to bind your buttons to commands that will be executed on click. To take advantage of the ICommandSource implementation, use the following properties:
-
Command—Gets the command that will be executed when the command source is invoked. -
CommandParameter—Represents a user-defined data value that can be passed to the command when it is executed. -
CommandTarget—The object on which the command is being executed.
RadButtons implement the ICommandSource, so you can use them with any command that implements the ICommand interface. For example, the DelegateCommand class.
Using Commands in MVVM
The following example demonstrates how to use commands in an MVVM scenario.
-
Define the command in a sample view model class.
C#public class SampleViewModel { public SampleViewModel() { MyCommand = new DelegateCommand(OnCommandExecuted); } public ICommand MyCommand { get; set; } private void OnCommandExecuted(object obj) { MessageBox.Show("MyCommand Executed"); } } -
Set a
SampleViewModelobject instance as theDataContextof your corresponding view (the UserControl or Window).C#public Example() { InitializeComponent(); this.DataContext = new SampleViewModel(); } -
In the XAML set the bindings for the command and set the command parameter.
XAML<telerik:RadButton Content="My Button" Command="{Binding MyCommand}" CommandParameter="ParameterValue" />RadButtonis located in theTelerik.Windows.Controls.dllassembly. To use them in your project, add a reference to the assembly or install theTelerik.Windows.Controls.for.Wpf.XamlNuGet package (or its NoXaml alternative). For more information, see the Controls Dependencies article.Then in XAML you have to declare the namespace:
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
This is a very basic sample, but you can apply this approach to any type of commands that implement the ICommand interface.