CompositeCommandBehavior Class

Allows you to aggregate multiple commands and execute them in a specific order.

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v21.2.dll

Declaration

public class CompositeCommandBehavior :
    Behavior<DependencyObject>

Remarks

CompositeCommandBehavior creates a composite command to store aggregated commands. The behavior binds a CompositeCommand to the associated control’s Command property. If the associated control does not have a Command property, assign the name of an ICommand property to CommandPropertyName.

You can use the CanExecuteCondition property to select a condition that allows the behavior to execute the composite command.

Follow the steps below to use the CompositeCommandBehavior:

  1. Define the behavior for a target control.

    <UserControl ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" />
        <Grid>
            <dxb:BarButtonItem Content="Save and Close">
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:CompositeCommandBehavior>
                    </dxmvvm:CompositeCommandBehavior>
                </dxmvvm:Interaction.Behaviors> 
            </dxb:BarButtonItem>
        </Grid>
    </UserControl>
    
  2. Create commands (CommandItem objects) and add them to the behavior.

    <UserControl ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" />
        <Grid>
            <dxb:BarButtonItem Content="Save and Close">
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:CompositeCommandBehavior>
                        <dxmvvm:CommandItem />
                        <dxmvvm:CommandItem />
                    </dxmvvm:CompositeCommandBehavior>
                </dxmvvm:Interaction.Behaviors> 
            </dxb:BarButtonItem>
        </Grid>
    </UserControl>
    
  3. Specify the Command property for each command to bind it to a ViewModel’s command. The behavior executes commands in the order you specify them.

    <UserControl ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" />
        <Grid>
            <dxb:BarButtonItem Content="Save and Close">
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:CompositeCommandBehavior>
                        <dxmvvm:CommandItem Command="{Binding SaveCommand}"/>
                        <dxmvvm:CommandItem Command="{Binding CloseCommand}"/>
                    </dxmvvm:CompositeCommandBehavior>
                </dxmvvm:Interaction.Behaviors> 
            </dxb:BarButtonItem>
        </Grid>
    </UserControl>
    
  4. Optional. Set the behavior’s CanExecuteCondition property to AnyCommandCanBeExecuted to execute the composite command when at least one of its aggregated commands is executable (the CommandItem.CanExecute property value is true). The default CanExecuteCondition property value is AllCommandsCanBeExecuted.

Example

The following code sample executes Save and Close commands when a user clicks the “Save and Close“ button:

<UserControl 
<!-- ... -->
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    <!-- ... -->
    <UserControl.DataContext>
        <vm:MainViewModel/>
    </UserControl.DataContext>
    <dxmvvm:Interaction.Behaviors>
        <dx:DXMessageBoxService />
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <dxb:BarManager>
            <dxb:BarManager.Bars>
                <dxb:Bar Caption="Main Menu" IsMainMenu="True">
                    <dxb:BarButtonItem Content="Save" Command="{Binding SaveCommand}"/>
                    <dxb:BarButtonItem Content="Close" Command="{Binding CloseCommand}"/>
                    <dxb:BarButtonItem Content="Save and Close">
                        <dxmvvm:Interaction.Behaviors>
                            <dxmvvm:CompositeCommandBehavior>
                                <dxmvvm:CommandItem Command="{Binding SaveCommand}"/>
                                <dxmvvm:CommandItem Command="{Binding CloseCommand}"/>
                            </dxmvvm:CompositeCommandBehavior>
                        </dxmvvm:Interaction.Behaviors> 
                    </dxb:BarButtonItem>
                </dxb:Bar>
            </dxb:BarManager.Bars>
            <Grid>
                <TextBox Text="{Binding Text,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AcceptsReturn="True"/>
            </Grid>
        </dxb:BarManager>
    </Grid>
</UserControl>
public virtual string Text { get; set; }
public virtual string SavedText { get; set; }
bool IsSaved { get { return SavedText == Text; } }
protected IMessageBoxService MessageService { get { return GetService<IMessageBoxService>(); } }
[Command]
public void Save() {
    SavedText = Text;
}
public bool CanSave() {
    return !IsSaved;
}
[Command]
public void Close() {

Run Demo: Behaviors Module in the WPF MVVM Demo

View Example: WPF MVVM Framework - Execute Multiple Commands with CompositeCommandBehavior

Inheritance

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