Back to Devexpress

CompositeCommandBehavior Class

wpf-devexpress-dot-mvvm-dot-ui-059c00eb.md

latest7.5 KB
Original Source

CompositeCommandBehavior Class

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

Namespace : DevExpress.Mvvm.UI

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

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public class CompositeCommandBehavior :
    Behavior<DependencyObject>
vb
Public Class CompositeCommandBehavior
    Inherits Behavior(Of 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.

  2. Create commands (CommandItem objects) and add them to the behavior.

  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.

  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:

xaml
<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>
csharp
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

The following code snippets (auto-collected from DevExpress Examples) contain references to the CompositeCommandBehavior class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-bind-gridcontrol-to-dynamic-data/CS/ITypedList/MainWindow.xaml#L27

xml
<dxmvvm:Interaction.Behaviors>
    <dxmvvm:CompositeCommandBehavior>
        <dxmvvm:CommandItem Command="{Binding AddColumnCommand}" />

wpf-data-grid-specify-row-visibility-in-viewmodel/CS/CustomFilteringMVVM/MainWindow.xaml#L41

xml
<dxmvvm:Interaction.Behaviors>
    <dxmvvm:CompositeCommandBehavior>
        <dxmvvm:CommandItem Command="{Binding AddExclusionCommand}"/>

wpf-scheduler-create-regular-and-recurring-appointments-at-view-model-level/CS/DXApplication14/MainWindow.xaml#L21

xml
<dxmvvm:Interaction.Behaviors>
    <dxmvvm:CompositeCommandBehavior CanExecuteCondition="AnyCommandCanBeExecuted">
        <dxmvvm:CommandItem Command="{Binding AddApptCommand}"

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

CompositeCommandBehavior Members

DevExpress.Mvvm.UI Namespace