Example T145641
Visible to All Users

Use DialogService to Show a Modal Dialog Window

This example demonstrates how you can use the DialogService to show a modal dialog window (ThemedWindow) and get its result.

image

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

View/MainView.xaml
XAML
<UserControl x:Class="Example.View.MainView" 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" xmlns:ViewModel="clr-namespace:Example.ViewModel" xmlns:View="clr-namespace:Example.View"> <UserControl.DataContext> <ViewModel:MainViewModel/> </UserControl.DataContext> <dxmvvm:Interaction.Behaviors> <dx:DialogService DialogWindowStartupLocation="CenterOwner"> <dx:DialogService.ViewTemplate> <DataTemplate> <View:RegistrationView/> </DataTemplate> </dx:DialogService.ViewTemplate> <dx:DialogService.DialogStyle> <Style TargetType="dx:ThemedWindow"> <Setter Property="Width" Value="300"/> <Setter Property="Height" Value="160"/> </Style> </dx:DialogService.DialogStyle> </dx:DialogService> <dx:DXMessageBoxService/> </dxmvvm:Interaction.Behaviors> <Grid Background="White"> <Button HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding ShowRegistrationFormCommand}" Content="Show Registration Form"/> </Grid> </UserControl>
View/RegistrationView.xaml
XAML
<UserControl x:Class="Example.View.RegistrationView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="User Name: "/> <TextBox Width="200" Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}"> <dxmvvm:Interaction.Behaviors> <dxmvvm:FocusBehavior/> </dxmvvm:Interaction.Behaviors> </TextBox> </StackPanel> </UserControl>
ViewModel/MainViewModel.cs(vb)
C#
using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; using System; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using System.Windows; namespace Example.ViewModel { public class MainViewModel : ViewModelBase { RegistrationViewModel registrationViewModel; IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } } IDialogService DialogService { get { return GetService<IDialogService>(); } } public MainViewModel() { registrationViewModel = new RegistrationViewModel(); } [Command] public void ShowRegistrationForm() { UICommand registerAsyncCommand = new UICommand( id: null, caption: "Register Async", command: new AsyncCommand<CancelEventArgs>( async cancelArgs => { try { await MyAsyncExecuteMethod(); } catch (Exception e) { MessageBoxService.ShowMessage(e.Message, "Error", MessageButton.OK); cancelArgs.Cancel = true; } }, cancelArgs => !string.IsNullOrEmpty(registrationViewModel.UserName) ), asyncDisplayMode: AsyncDisplayMode.Wait, isDefault: false, isCancel: false ); UICommand registerCommand = new UICommand( id: null, caption: "Register", command: new DelegateCommand<CancelEventArgs>( cancelArgs => { try { MyExecuteMethod(); } catch (Exception e) { MessageBoxService.ShowMessage(e.Message, "Error", MessageButton.OK); cancelArgs.Cancel = true; } }, cancelArgs => !string.IsNullOrEmpty(registrationViewModel.UserName) && !((IAsyncCommand)registerAsyncCommand.Command).IsExecuting ), isDefault: true, isCancel: false ); UICommand cancelCommand = new UICommand( id: MessageBoxResult.Cancel, caption: "Cancel", command: null, isDefault: false, isCancel: true ); UICommand result = DialogService.ShowDialog( dialogCommands: new List<UICommand>() { registerAsyncCommand, registerCommand, cancelCommand }, title: "Registration Dialog", viewModel: registrationViewModel ); if (result == registerCommand) { // ... } } void MyExecuteMethod() { // ... } async Task MyAsyncExecuteMethod() { await Task.Delay(2000); // ... } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.