Use Commands with RadRibbonView Buttons

All RadRibbonView buttons provide a standard ICommandSource implementation. This is why you can bind the RadRibbonButtons to commands that will get executed when a button is clicked. For this purpose you can 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 all butons implement the ICommandSource interface, you can use them with any command that implements the ICommand interface, for example the DelegateCommands.

Here is an example of a command usage in a MVVM scenario. The command is located in the ButtonViewModel class.

public class ButtonViewModel 
{ 
    public ButtonViewModel() 
    { 
        this.MyCommand = new DelegateCommand(p => Execute(p), p => CanExecute(p)); 
    } 
 
    public DelegateCommand MyCommand { get; set; } 
 
    private bool CanExecute(object p) 
    { 
        if (p != null) 
        { 
            return true; 
        } 
        else 
        { 
            return false; 
        } 
    } 
 
    private void Execute(object p) 
    { 
        MessageBox.Show("Executed: \n" + p.ToString()); 
    } 
} 
Public Class ButtonViewModel 
    Public Sub New() 
        Me.MyCommand = New DelegateCommand(Function(p) Execute(p), Function(p) CanExecute(p)) 
    End Sub 
 
    Public Property MyCommand() As DelegateCommand 
        Get 
            Return m_MyCommand 
        End Get 
        Set(value As DelegateCommand) 
            m_MyCommand = Value 
        End Set 
    End Property 
    Private m_MyCommand As DelegateCommand 
 
    Private Function CanExecute(p As Object) As Boolean 
        If p IsNot Nothing Then 
            Return True 
        Else 
            Return False 
        End If 
    End Function 
 
    Private Sub Execute(p As Object) 
        MessageBox.Show("Executed: " & vbLf + p.ToString()) 
    End Sub 
End Class 

Set the ButtonViewModel as the DataContext of your UserControl.

public Example() 
{ 
    InitializeComponent(); 
    this.DataContext = new ButtonViewModel(); 
} 
Public Sub New() 
    InitializeComponent() 
    Me.DataContext = New ButtonViewModel() 
End Sub 

In the XAML provide the bindings for the command and set the command parameter.

This can be applied to each of the RadRibbonButtons. To learn more about them read here. Commands can be used with any control, placed in the RadRibbonView, that implements the ICommandSource interface.

<telerik:RadRibbonView x:Name="xRibbonView"> 
    <telerik:RadRibbonTab Header="Home"> 
        <telerik:RadRibbonGroup Header="Home Group"> 
            <telerik:RadRibbonButton Width="Auto" 
                                     Height="20" 
                                     Command="{Binding MyCommand}" 
                                     CommandParameter="ParameterValue" 
                                     Content="Execute Command" /> 
        </telerik:RadRibbonGroup> 
    </telerik:RadRibbonTab> 
</telerik:RadRibbonView> 

See Also

In this article