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.

As the RadButtons implement the ICommandSource, you can use them with any command that implements the ICommand interface, for example, the Telerik DelegateCommand.

Example

The following example demonstrates how to use commands in an MVVM scenario.

  1. Define the command. The command is located in the SampleViewModel class:

    Example 1: Defining a command

    public class SampleViewModel 
    { 
        public SampleViewModel() 
        { 
            MyCommand = new DelegateCommand(OnCommandExecuted); 
        } 
        public ICommand MyCommand { get; set; } 
     
        private void OnCommandExecuted(object obj) 
        { 
            MessageBox.Show("MyCommand Executed"); 
        }  
    } 
    
        Public Class SampleViewModel 
            Public Sub New() 
                MyCommand = New DelegateCommand(AddressOf OnCommandExecuted) 
            End Sub 
     
            Public Property MyCommand As ICommand 
     
            Private Sub OnCommandExecuted(ByVal obj As Object) 
                MessageBox.Show("MyCommand Executed") 
            End Sub 
        End Class 
    
  2. Set the SampleViewModel as the DataContext of your UserControl:

    Example 2: Setting up the DataContext

        public Example() 
        { 
            InitializeComponent(); 
            this.DataContext = new SampleViewModel(); 
        } 
    
        Public Sub New() 
            InitializeComponent() 
            Me.DataContext = New SampleViewModel() 
        End Sub 
    
  3. In the XAML provide the bindings for the command and set the command parameter:

    Example 3: Setting up the Command and CommandParameter properties

        <telerik:RadButton Content="My Button" 
                        Command="{Binding MyCommand}" 
                        CommandParameter="ParameterValue" /> 
    

    RadButtons are located in the Telerik.Windows.Controls.dll assembly. To use them in your project, add a reference to the assembly. 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.

See Also

In this article