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

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—The ICommand object that will be executed on button click.

  • CommandParameter—The object that will be passed as the parameter of the Execute method of the command.

  • CommandTarget—The UIElement on 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 ICommand interface and allows you to provide two actions - Execute and CanExecute.

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.

  1. 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 
            }  
        } 
    
  2. 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(); 
        } 
    
  3. 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

  4. Getting Started
  5. Events
In this article
Not finding the help you need?