This example demonstrates how you can use the DialogService to show a modal dialog window (ThemedWindow) and get its result.
Files to Review
- MainView.xaml (VB: MainView.xaml)
- RegistrationView.xaml (VB: RegistrationView.xaml)
- MainViewModel.cs (VB: MainViewModel.vb)
- RegistrationViewModel.cs (VB: RegistrationViewModel.vb)
Documentation
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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>
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>
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);
// ...
}
}
}