[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]ViewModels
  [Expand]Commands
  [Expand]Behaviors
  [Collapse]Services
    Getting Started
    Services in ViewModelBase descendants
    Services in POCO objects
    Services in custom ViewModels
    View creation mechanisms
   [Collapse]Predefined Set
    [Collapse]Dialog Services
      DialogService
      WinUIDialogService
    [Expand]Message Box Services
    [Expand]Document Services
    [Expand]ViewInjectionService
    [Expand]Report Services
     NotificationService
     TaskbarButtonService
     ApplicationJumpListService
     DXSplashScreenService
     DispatcherService
     FrameNavigationService
     LayoutSerializationService
     FolderBrowserDialogService
     OpenFileDialogService
     SaveFileDialogService
     WizardService
     NotifyIconService
    How to create a Custom Service
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

DialogService

DialogService is a IDialogService implementation that allows you to show a modal window (the DXDialogWindow control).

Expanded Getting started with DialogService

Assume that you need to show a modal window for user registration. The window should contain two buttons: Register and Cancel. The Register button should be disabled if the input data is invalid. When the window is closed, registration results should be passed to the main View Model. For this task, the DevExpress MVVM Framework provides the IDialogService interface and its implementation – DialogService.

Suppose that there is the following Main View with the Show Registration Form button.

The button is bound to the MainViewModel command (MainViewModel is a POCO object, so there is no need to create command properties).

When the ShowRegistrationForm command is invoked, it is necessary to show a dialog window that contains the following View.

The RegistrationView contains only one editor bound to the RegistrationViewModel. Please note that we do not set the View Model for the RegistrationView directly. You will create a View Model for the RegistrationView at the main View Model level. This mechanism is described below. If you need to set a design-time View Model for your view, use the d:DataContext property.

The RegistrationViewModel is a POCO class and its implementation is as follows.

Next, you will implement display a dialog window that contains the RegistrationView. To do this, add the DialogService to the main View as follows.

Next, you will use the defined DialogService from our View Model.

As you can see in the above code snippet, the dialog window is shown via the ShowDialog method. The IDialogService has several extension methods for different scenarios. For instance, to set standard buttons for the dialog, use the following extension.

The task you are solving is more complex, so you will use another extension that allows setting custom buttons.

This method takes a collection of UICommand objects as a parameter. In fact, the UICommand is a ready-to-use View Model for a dialog button. Thus, create two UICommands at the MainViewModel level and pass these UICommands to the ShowDialog method as it shown below.

Note that dialog commands take a CancelEventArgs object as a parameter. When dialog commands are invoked, the dialog is closed by default. To prevent this behavior, it is necessary to set the CancelEventArgs.Cancel parameter to True. For instance:

When the registration process is finished and the dialog is closed, the ShowDialog method returns the command that closed the dialog. After that, you can implement the necessary logic at the main View Model level.

Expanded View creation mechanism

The IDialogService interface has only one method for showing a view in a modal window.

The last four parameters are used when the DialogService creates a child View. There is no need to use all four parameters simultaneously, so the method is hidden. Instead, there are several extension methods.

  • Method 1 and 2 - are used when a child View is defined though the DialogService.ViewTemplate or DialogService.ViewTemplateSelector property. The child View should not contain a View Model, because it is passed through the service.
  • Method 3 and 4 - these methods use the ViewLocator to create a child View and passes the specified View Model to the created child View.
  • Method 5 and 6 - these methods create a child View through the ViewLocator. The child View already contains a View Model. The Parameter and ParentViewModel are passed to the child View Model.

Please review the following documentation article to learn more: View creation mechanisms

Expanded DialogService properties

The DialogService provides several properties for the customization of the dialog window.

  • The ViewTemplate is used for setting a View that will be shown inside the dialog window.
  • The ViewTemplateSelector sets a DataTemplateSelector that returns a certain ViewTemplate by the passed View Model.
  • The ViewLocator sets a ViewLocator that creates a child View based on the passed view type.
  • The DialogStyle sets a Style for the dialog window.
  • The DialogWindowStartupLocation sets window's startup position.

The first three properties are used by the view creation mechanism. You can learn more about it in the following topic: View creation mechanisms.

The DialogStyle property sets a style for the DXDialogWindow control that is used as a container for showing child Views. You can set this property in the following manner:

The last DialogWindowStartupLocation property sets the window's startup position. This property is added to the DialogService, because the Window.WindowStartupLocation is not a dependency property and it cannot be set from a Window style.

Expanded Dialog buttons generation mechanism

There are two approaches to set dialog buttons. The first and the simplest method is to pass a MessageButton object to the IDialogService.ShowDialog method. The MessageButton is an enumerable that has four values: OK, OKCancel, YesNo, YesNoCancel. Based on which value is passed through the ShowDialog method, the DialogService generates one, two, or three dialog buttons. When the displayed dialog is closed, the ShowDialog method returns a value of the MessageResult enumerable type (None, OK, Cancel, Yes, No). If the dialog was closed by pressing the OK button, then the dialog result is OK. If the dialog was closed with the window close button, then the dialog result is None.

The second approach is to pass a collection of UICommand objects to the IDialogService.ShowDialog method. Based on the passed UICommands, the DialogService generates the Button controls. When the shown dialog is closed, the ShowDialog method returns a UICommand of the button which was used for closing the dialog (or null if the dialog was closed by the window close button). The UICommand class provides the following properties:

  • Caption (Object) - specifies the Button content.
  • Command (ICommand) - specifies the Button command. The command may take a command parameter of the CancelEventArgs type. When a command is invoked, you can set the e.Cancel parameter to True to prevent closing the dialog.
  • Id (Object) - is an identifier of a UICommand. You can use this property to determine the result UICommand when the dialog is closed.
  • IsCancel (Boolean) - specifies whether or not the generated Button should be "cancel button". Cancel buttons can be activated by pressing the ESC key.
  • IsDefault (Boolean) - specifies whether or not the generated Button should be "default button". A user invokes the default button by pressing the ENTER key.
  • Tag (Object) - is a property that can be used to store custom information.

With UICommands, you can also set a style of generated buttons. For instance:

Expanded Example

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T145641.

Is this topic helpful?​​​​​​​