Skip to main content

MethodToCommandBehavior Class

Binds a method of a ViewModel or control to a Command property of a control on a View.

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v24.1.dll

NuGet Package: DevExpress.Wpf.Core

Declaration

public class MethodToCommandBehavior :
    FunctionBindingBehaviorBase

Remarks

Specify the following MethodToCommandBehavior properties to bind a ViewModel (or a control) method to a Command property of a control in a View:

Target
An object whose Command property should be bound to a Method. The default target object is the behavior’s associated object. This is a dependency property.
Command
Gets or sets a Target object’s command property name that should be bound to the specified Method. This is a dependency property.
Source
Gets or sets an object whose method should be executed. This is a dependency property.
Method
Gets or sets the name of a Source object’s method that should be invoked. This is a dependency property.
CanExecuteFunction (Optional)
Gets or sets the name of a function that determines whether the Method can be executed. This is a dependency property.
Arg* Properties (Optional)

Properties that the behavior should pass to the Method as arguments. You can specify no more than 15 arguments.

The MethodToCommandBehavior converts the specified arguments to the parameterized type, if possible.

The following code sample binds the GridControl‘s SortBy method to the BarButtonItem and passes the GridControl‘s CurrentColumn and Descending sort order properties:

<UserControl ...
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <Grid>
        <dxb:BarManager>
            <dxb:BarManager.Bars>
                <dxb:Bar Caption="Column Sorting Bar">
                    <dxb:BarButtonItem 
                        Content="Descending" 
                        Glyph="{dx:DXImage Image=MoveDown_16x16.png}" 
                        BarItemDisplayMode="ContentAndGlyph">               
                        <dxmvvm:Interaction.Behaviors>
                            <dxmvvm:MethodToCommandBehavior 
                                Source="{Binding ElementName=gridControl}" 
                                Method="SortBy"
                                Arg1="{Binding ElementName=gridControl,Path=CurrentColumn}" 
                                Arg2="Descending"/>
                        </dxmvvm:Interaction.Behaviors>
                    </dxb:BarButtonItem>
                </dxb:Bar>
            </dxb:BarManager.Bars>
            <Grid>
                <dxg:GridControl x:Name="gridControl" ...>
                    <dxg:GridControl.View>
                        <dxg:TableView x:Name="tableView" ... />
                    </dxg:GridControl.View>
                </dxg:GridControl>
            </Grid>
        </dxb:BarManager>
    </Grid>
</UserControl>

Inheritance

Object
DispatcherObject
DependencyObject
Freezable
Animatable
DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase
DevExpress.Mvvm.UI.Interactivity.Behavior
DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject>
FunctionBindingBehaviorBase
MethodToCommandBehavior
See Also