Back to Devexpress

ConfirmationBehavior Class

wpf-devexpress-dot-mvvm-dot-ui-83d09fa8.md

latest6.8 KB
Original Source

ConfirmationBehavior Class

Shows a confirmation message box before an application executes the specified command. Users can confirm or cancel the operation.

Namespace : DevExpress.Mvvm.UI

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

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public class ConfirmationBehavior :
    Behavior<DependencyObject>
vb
Public Class ConfirmationBehavior
    Inherits Behavior(Of DependencyObject)

Remarks

The following code sample attaches a command to a Button and enables a confirmation message for this command:

xaml
<UserControl x:Class="Example.View.MainView"
<!-- ... -->
    xmlns:ViewModel="clr-namespace:Example.ViewModel"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    <!-- ... -->
    <UserControl.Resources>
        <dxmvvm:BooleanNegationConverter x:Key="BooleanNegationConverter"/>
        <dxmvvm:BooleanToObjectConverter x:Key="BooleanToObjectConverter" TrueValue="Saved!" FalseValue="Unsaved!"/>
    </UserControl.Resources>

    <UserControl.DataContext>
        <ViewModel:MainViewModel/>
    </UserControl.DataContext>

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Content="Save" Command="{Binding SaveCommand}"/>
            <Button Content="Close">
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:ConfirmationBehavior EnableConfirmationMessage="{Binding IsSaved, Converter={StaticResource BooleanNegationConverter}}" 
                                                 Command="{Binding CloseCommand}" MessageText="The document has unsaved changes. Do you want to close the document?"/>
                </dxmvvm:Interaction.Behaviors>
            </Button>
            <TextBlock Text="{Binding IsSaved, Converter={StaticResource BooleanToObjectConverter}}"/>
        </StackPanel>
        <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Row="1"/>
    </Grid>
</UserControl>
csharp
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
// ...
    public class MainViewModel : ViewModelBase {

        string savedText;

        public string Text {
            get => GetValue<string>();
            set {
                SetValue(value);
                RaisePropertyChanged(nameof(IsSaved));
            }
        }
        // ...
        [Command]
        public void Close() {
            savedText = string.Empty;
            Text = string.Empty;
        }
    }
vb
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.DataAnnotations
' ...
    Public Class MainViewModel
        Inherits ViewModelBase

        Private savedText As String

        Public Property Text As String
            Get
                Return GetValue(Of String)()
            End Get

            Set(ByVal value As String)
                SetValue(value)
                RaisePropertyChanged(NameOf(MainViewModel.IsSaved))
            End Set
        End Property
        ' ...
        <Command>
        Public Sub Close()
            savedText = String.Empty
            Text = String.Empty
        End Sub
    End Class

Run Demo: Behaviors Module in the WPF MVVM Demo

View Example

Customize a Confirmation Box

PropertyDesciption
CommandGets or sets the command that runs if the user accepts the confirmation message. This is a dependency property.
CommandPropertyNameGets or sets a name of an associated control’s command property. This is a dependency property.
CommandParameterGets or sets parameters to pass to the Command. This is a dependency property.
EnableConfirmationMessageGets or sets whether a confirmation message should be displayed. This is a dependency property.
MessageBoxServiceGets or sets a custom message box service. You can use the WinUIMessageBox or create a custom message box that implements the IMessageBoxService interface. This is a dependency property.
MessageDefaultResultGets or sets the confirmation message button that should be focused when the message appears. This is a dependency property.
MessageButtonGets or sets the confirmation message buttons. This is a dependency property.
MessageIconGets or sets the confirmation message icon. This is a dependency property.
MessageTextGets or sets the confirmation message text. This is a dependency property.
MessageTitleGets or sets the confirmation message title. This is a dependency property.

Inheritance

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject> ConfirmationBehavior

See Also

ConfirmationBehavior Members

Behaviors

DevExpress.Mvvm.UI Namespace