CompositeCommandBehavior Class
Allows you to aggregate multiple commands and execute them in a specific order.
Namespace: DevExpress.Mvvm.UI
Assembly: DevExpress.Xpf.Core.v24.1.dll
NuGet Package: DevExpress.Wpf.Core
Declaration
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:
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>
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>
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>
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() {
Related GitHub Examples
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.