Skip to main content
A newer version of this page is available. .

ConfirmationBehavior

  • 4 minutes to read

It’s often required to show a confirmation box before performing an action. ConfirmationBehavior allows to automate this process.

Assume that there is a form with several documents. When an end-user closes an unsaved document, it is required to show a message: “Are you sure to close the unsaved document?”.

To accomplish this task, you can bind the Close button to a command with the ConfirmationBehavior in the following manner.

<Button Content="Close">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:ConfirmationBehavior EnableConfirmationMessage="{Binding IsSaved, Converter={StaticResource BooleanNegationConverter}}" 
             Command="{Binding CloseCommand}" MessageText="Are you sure to close the unsaved document?"/>
    </dxmvvm:Interaction.Behaviors>
</Button>

As you can see from the example above, the Button (associated control) is not bound to a command directly. Instead, it is necessary to bind the ConfirmationBehavior.Command property. In this case, the ConfirmationBehavior will set the command property of the associated control automatically and when the command is invoked, it shows a confirmation message, if required.

If the associated control does not have the Command property, you can set the ConfirmationBehavior.CommandPropertyName. This property determines what property the ConfirmationBehavior should use as a Command property of the associated control.

To control whether or not the confirmation message should be shown when the bound command is executed, set the EnableConfirmationMessage property. If this property equals False, the ConfirmationBehavior does not show the confirmation message.

The ConfirmationBehavior class provides several properties for customizing the confirmation message:

  • MessageTitle - specifies the title of the confirmation message.
  • MessageText - specifies the text of the confirmation message.
  • MessageIcon - this property has the enum type (MessageBoxImage) and specifies the icon of the confirmation message.
  • MessageButton - this property has the enum type (MessageBoxButton) and specifies which buttons should be shown in the confirmation messages.
  • MessageDefaultResult - specifies the default result of the confirmation message.

By default, the ConfirmationBehavior uses the DXMessageBoxService for showing a confirmation message. If you need to use a custom IMessageBoxService (for instance, WinUIMessageBoxService), you can define a certain message box service and bind the ConfirmationBehavior.MessageBoxService property as shown below.

<Button Content="Close">
    <dxmvvm:Interaction.Behaviors>
        <dxwui:WinUIMessageBoxService 
             xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui"
             x:Name="winUIMessageBoxService"/>
        <dxmvvm:ConfirmationBehavior EnableConfirmationMessage="{Binding IsSaved, Converter={StaticResource BooleanNegationConverter}}" 
             Command="{Binding CloseCommand}" MessageText="Are you sure to close the unsaved document?"
             MessageBoxService="{Binding ElementName=winUIMessageBoxService}"/>
    </dxmvvm:Interaction.Behaviors>
</Button>

Example

using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm.POCO;

namespace Example.ViewModel {
    [POCOViewModel]
    public class MainViewModel {
        string SavedText;
        public virtual string Text { get; set; }
        public bool IsSaved {
            get {
                return Text == SavedText;
            }
        }
        public void Save() {
            SavedText = Text;
            this.RaisePropertyChanged(x => x.IsSaved);
        }
        public void Close() {
            SavedText = "";
            Text = "";
        }
        protected void OnTextChanged() {
            this.RaisePropertyChanged(x => x.IsSaved);
        }
    }
}