• 4 minutes to read

The MethodToCommandBehavior class is special behavior that allows you to bind a method to a property of the ICommand type.

Getting Started

Assume that your task is to invoke GridControl methods by clicking the BarButtonItems located in the View.

Below is a code snippet of the View.

                Glyph="{dx:DXImage Image=MoveDown_16x16.png}" 
                Glyph="{dx:DXImage Image=MoveUp_16x16.png}" 
                Content="Clear Sorting" 
                Glyph="{dx:DXImage Image=Clear_16x16.png}" 
            ItemsSource="{Binding Users}" 
            AutoGenerateColumns="AddNew" >

The solution is to use the MethodToCommandBehavior. Attach this behavior to a BarButtonItem, then specify the MethodToCommandBehavior properties step by step.

  • The Target property is responsible for the target-object. In the current scenario, the target object is the BarButtonItem. By default, the MethodToCommandBehavior uses the associated object as the Target, so if you attach the behavior to the required object directly, you can skip the Target initialization.
  • The Command property specifies the name of a property that should contain the ICommand object, which created by the MethodToCommandBehavior based on the specified Method, CanExecuteFunction and Args properties. Its default value is "Command".
  • The Source property should contain the source-object. Due to the fact that the source-method is owned by the GridControl, bind the Source property to the GridControl.
  • The Method property contains the source-method name. Therefore, you need to specify the name of the required method here.
  • The CanExecuteFunction property line allows you to specify a Boolean function that determines whether the method specified using the Method property line can be executed in its current state.
  • The MethodToCommandBehavior also provides several properties that are responsible for the source-method parameters: Arg1, Arg2... Arg15. You can specify no more than 15 arguments.
    Glyph="{dx:DXImage Image=MoveDown_16x16.png}" 
            Source="{Binding ElementName=gridControl}" 
            Arg1="{Binding ElementName=gridControl,Path=CurrentColumn}" 

Arguments Conversion

The MethodToCommandBehavior automatically converts its specified arguments to the parameterized type if possible. In the code above, the "Descending" string will be automatically converted to the ColumnSortOrder parameterized type.

Declaration via SmartTag

Assign the MethodToCommandBehavior to the corresponding BarButtonItem.


Specify the source-object. For this, bind the Source property to GridControl by using the Binding Editor.


Choose the required source-object's method from the dropdown list.


After the method has been selected, Smart Tag generates property lines for method's arguments. Specify them in order.


Note that Smart Tag generates property lines based on the maximum number of method's arguments. So, there is no need to specify them simultaneously.


Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports DevExpress.Mvvm.POCO

Namespace MethodToCommandExample.Common
    Public Class User
        Protected Sub New(ByVal name As String, ByVal iD As Integer)
            Me.Name = name
            Me.ID = iD
        End Sub
        Public Shared Function Create(ByVal id As Integer, ByVal name As String) As User
            Return ViewModelSource.Create(Function() New User(name, id))
        End Function

        Public Property Name() As String
        Public Property ID() As Integer
    End Class
End Namespace