Back to Devexpress

CurrentDialogService

wpf-401018-mvvm-framework-services-predefined-set-currentdialogservice.md

latest4.1 KB
Original Source

CurrentDialogService

  • Mar 19, 2023
  • 2 minutes to read

The CurrentDialogService allows you to control the associated dialog window and specify the dialog result in the Close method at the View Model level. The CurrentDialogService implements the ICurrentDialogService interface.

The table below lists the Close method overloads:

MethodAction
Close(MessageResult dialogResult)Closes the associated dialog window with a specified result of the MessageResult type
Close(UICommand dialogResult)Closes the associated dialog window with a specified result of the UICommand type and invokes UICommand.Command. You can use only one of the UICommands that were initially passed to the dialog service in the ShowDialog method.

Example

View Example

xaml
<UserControl
    x:Class="DialogServiceExample.Views.SimpleDialogView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">

    <dxmvvm:Interaction.Behaviors>
        <dx:CurrentDialogService />
    </dxmvvm:Interaction.Behaviors>

    <StackPanel>
        <ComboBox SelectedItem="{Binding DialogResult}">
            <ComboBox.Items>
                <dxmvvm:MessageResult>Yes</dxmvvm:MessageResult>
                <dxmvvm:MessageResult>No</dxmvvm:MessageResult>
                <dxmvvm:MessageResult>Cancel</dxmvvm:MessageResult>
            </ComboBox.Items>
        </ComboBox>
        <Button Command="{Binding CloseCommand}" Content="Close the dialog from the dialog view model" />
    </StackPanel>

</UserControl>
cs
using DevExpress.Mvvm;
using System.Windows.Input;

namespace DialogServiceExample.ViewModels {
    public class SimpleDialogViewModel : ViewModelBase {
        public MessageResult DialogResult {
            get { return GetProperty(() => DialogResult); }
            set { SetProperty(() => DialogResult, value); }
        }
        public ICommand CloseCommand { get; private set; }
        protected ICurrentDialogService CurrentDialogService { get { return GetService<ICurrentDialogService>(); } }

        public SimpleDialogViewModel() {
            CloseCommand = new DelegateCommand<MessageResult>(Close);
        }

        public void Close(MessageResult parameter) {
            CurrentDialogService.Close(DialogResult);
        }
    }
}
vb
Imports DevExpress.Mvvm

Namespace DialogServiceExample.ViewModels
    Public Class SimpleDialogViewModel
        Inherits ViewModelBase

        Public Property DialogResult() As MessageResult
            Get
                Return GetProperty(Function() DialogResult)
            End Get
            Set(ByVal value As MessageResult)
                SetProperty(Function() DialogResult, value)
            End Set
        End Property
        Public Property CloseCommand() As ICommand
        Protected ReadOnly Property CurrentDialogService() As ICurrentDialogService
            Get
                Return GetService(Of ICurrentDialogService)()
            End Get
        End Property

        Public Sub New()
            CloseCommand = New DelegateCommand(Of MessageResult)(AddressOf Close)
        End Sub

        Public Sub Close(ByVal parameter As MessageResult)
            CurrentDialogService.Close(DialogResult)
        End Sub
    End Class
End Namespace

See Also

CurrentWindowService