Skip to main content

DXMessageBoxService

  • 8 minutes to read

The DXMessageBoxService is an IMessageBoxService implementation that uses the ThemedMessageBox control to show messages.

DXMessageBox Collage

Note

Use the UseThemedWindowInServices compatibility property to use the DXMessageBox control in the DXMessageBoxService.

The following code sample demonstrates how to use the DXMessageBoxService and perform an action based on the clicked message box button:

<Window ...
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
        DataContext="{dxmvvm:ViewModelSource Type=local:ViewModel}">
    <dxmvvm:Interaction.Behaviors>
        <dx:DXMessageBoxService/>
    </dxmvvm:Interaction.Behaviors>

    <dx:SimpleButton Content="{Binding ButtonText}"
                     Command="{Binding SaveConfirmationCommand}"/>
</Window>
public class ViewModel : ViewModelBase {
    public virtual string ButtonText { get; set; } = "Save Changes Button";

    IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }

    [Command]
    public void SaveConfirmation() {
        MessageResult result;
        result = MessageBoxService.ShowMessage(
            messageBoxText: "Save Changes?",
            caption: "DXApplication",
            icon: MessageIcon.Question,
            button: MessageButton.YesNoCancel,
            defaultResult: MessageResult.Cancel
        );
        switch (result) {
            case MessageResult.Yes:
                ButtonText = "Changes Saved";
                break;
            case MessageResult.No:
                ButtonText = "Changes Discarded";
                break;
            case MessageResult.Cancel:
                ButtonText = "Continue Editing";
                break;
        }
    }
}

Below is the list of examples that describes how to use the DXMessageBoxService in different View Models:

How to use MessageBoxService in ViewModels derived from the ViewModelBase class

This example demonstrates how to use the DXMessageBoxService in View Models derived from the ViewModelBase class. The View Models are related to each other by the parent-child relationship with the ISupportParentViewModel interface.

Refer to the ViewModel relationships (ISupportParentViewModel) topic for more information on View Model parent-child relationships.

using DevExpress.Mvvm;
using System.Windows.Input;

namespace Example.ViewModel {
    public class ChildViewModel : ViewModelBase {
        public ICommand ShowMessageCommand { get; private set; }
        public ChildViewModel() {
            ShowMessageCommand = new DelegateCommand(ShowMessage);
        }

        IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(ServiceSearchMode.PreferParents); } }
        void ShowMessage() {
            MessageBoxService.Show("This is ChildView");
        }
    }
}

How to use MessageBoxService in POCO View Models

This example demonstrates how to use the DXMessageBoxService in POCO View Models. The View Models are related to each other by the parent-child relationship with the ISupportParentViewModel interface.

Refer to the ViewModel relationships (ISupportParentViewModel) topic for more information on View Model parent-child relationships.

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

namespace Example.ViewModel {
    public class ChildViewModel {
        [ServiceProperty(SearchMode=ServiceSearchMode.PreferParents)]
        protected virtual IMessageBoxService MessageBoxService { get { return null; } }
        public void ShowMessage() {
            MessageBoxService.Show("This is ChildView");
        }
    }
}

How to use MessageBoxService in a custom View Model

This example demonstrates how to use the DXMessageBoxService in a custom View Model (not derived from the ViewModelBase class and not a POCO View Model).

Refer to the Services in custom ViewModels topic for more information on how to use the Service mechanism in a custom View Model.

Custom View Models in this example are related to each other with the parent-child relationship. This is achieved by supporting the ISupportParentViewModel interface in the View Models.

Refer to the ViewModel relationships (ISupportParentViewModel) topic for more information on View Model parent-child relationships.

using DevExpress.Mvvm;
using System.Windows.Input;

namespace Example.ViewModel {
    public class ChildViewModel : ISupportServices, ISupportParentViewModel {
        IServiceContainer serviceContainer = null;
        protected IServiceContainer ServiceContainer {
            get {
                if(serviceContainer == null)
                    serviceContainer = new ServiceContainer(this);
                return serviceContainer;
            }
        }
        IServiceContainer ISupportServices.ServiceContainer { get { return ServiceContainer; } }
        object ISupportParentViewModel.ParentViewModel { get; set; }

        IMessageBoxService MessageBoxService { get { return ServiceContainer.GetService<IMessageBoxService>(ServiceSearchMode.PreferParents); } }

        public ICommand ShowMessageCommand { get; private set; }
        public ChildViewModel() {
            ShowMessageCommand = new DelegateCommand(ShowMessage);
        }
        void ShowMessage() {
            MessageBoxService.Show("This is ChildView");
        }
    }
}