■ DialogService 클래스를 사용하는 방법을 보여준다.
▶ DialogView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<UserControl x:Class="TestProject.DialogView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"> <Grid Margin="10"> <StackPanel Orientation="Vertical"> <StackPanel Margin="10" Orientation="Horizontal"> <TextBlock Text="Name : " /> <dxe:TextEdit Width="200" ValidateOnTextInput="True" EditValue="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" /> </StackPanel> <dxe:CheckEdit Margin="10" Content="Can Close Dialog" EditValue="{Binding CanCloseDialog}" /> </StackPanel> </Grid> </UserControl> |
▶ DialogView.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System.Windows.Controls; namespace TestProject { /// <summary> /// 대화 상자 뷰 /// </summary> public partial class DialogView : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DialogView() /// <summary> /// 생성자 /// </summary> public DialogView() { InitializeComponent(); } #endregion } } |
▶ DialogViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
using System.Collections.Generic; using System.ComponentModel; using System.Windows; using DevExpress.Xpf.Mvvm; namespace TestProject { /// <summary> /// 대화 상자 뷰 모델 /// </summary> public class DialogViewModel : ViewModelBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사용자명 /// </summary> private string _strUserName; /// <summary> /// 대화 상자 닫기 가능 여부 /// </summary> private bool _bCanCloseDialog = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 사용자명 - UserName /// <summary> /// 사용자명 /// </summary> public string UserName { get { return _strUserName; } set { SetProperty(ref _strUserName, value, () => UserName); } } #endregion #region 대화 상자 닫기 가능 여부 - CanCloseDialog /// <summary> /// 대화 상자 닫기 가능 여부 /// </summary> public bool CanCloseDialog { get { return _bCanCloseDialog; } set { SetProperty(ref _bCanCloseDialog, value, () => CanCloseDialog); } } #endregion #region 대화 상자 UI 명령 리스트 - DialogUICommandList /// <summary> /// 대화 상자 UI 명령 리스트 /// </summary> public List<UICommand> DialogUICommandList { get; private set; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region Register UI 명령 - RegisterUICommand /// <summary> /// Register UI 명령 /// </summary> protected UICommand RegisterUICommand { get; private set; } #endregion #region Cancel UI 명령 - CancelUICommand /// <summary> /// Cancel UI 명령 /// </summary> protected UICommand CancelUICommand { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DialogViewModel() /// <summary> /// 생성자 /// </summary> public DialogViewModel() { DialogUICommandList = new List<UICommand>(); RegisterUICommand = new UICommand() { Caption = "Register", Command = new DelegateCommand<CancelEventArgs>(OnRegisterUICommandExecute, OnRegisterUICommandCanExecute), IsDefault = true, Id = MessageBoxResult.OK, }; CancelUICommand = new UICommand() { Caption = "Cancel", Command = new DelegateCommand<CancelEventArgs>(OnCancelUICommandExecute, OnCancelUICommandCanExecute), IsCancel = true, Id = MessageBoxResult.Cancel, }; DialogUICommandList.Add(RegisterUICommand); DialogUICommandList.Add(CancelUICommand); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Register UI 명령 실행시 처리하기 - OnRegisterUICommandExecute(e) /// <summary> /// Register UI 명령 실행시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> private void OnRegisterUICommandExecute(CancelEventArgs e) { if(!CanCloseDialog) { e.Cancel = true; } } #endregion #region Register UI 명령 실행 가능 여부 조사시 처리하기 - OnRegisterUICommandCanExecute(e) /// <summary> /// Register UI 명령 실행 가능 여부 조사시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <returns>Register UI 명령 실행 가능 여부</returns> private bool OnRegisterUICommandCanExecute(CancelEventArgs e) { return !string.IsNullOrEmpty(UserName); } #endregion #region Cancel UI 명령 실행시 처리하기 - OnCancelUICommandExecute(e) /// <summary> /// Cancel UI 명령 실행시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> private void OnCancelUICommandExecute(CancelEventArgs e) { if(!CanCloseDialog) { e.Cancel = true; } } #endregion #region Cancel UI 명령 실행 가능 여부 조사시 처리하기 - OnCancelUICommandCanExecute(e) /// <summary> /// Cancel UI 명령 실행 가능 여부 조사시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <returns>Cancel UI 명령 실행 가능 여부</returns> private bool OnCancelUICommandCanExecute(CancelEventArgs e) { return true; } #endregion } } |
▶ MainViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
using System.Windows.Input; using DevExpress.Xpf.Mvvm; namespace TestProject { /// <summary> /// 메인 뷰 모델 /// </summary> public class MainViewModel : ViewModelBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region Show Dialog 명령 - ShowDialogCommand /// <summary> /// Show Dialog 명령 /// </summary> public ICommand ShowDialogCommand { get; private set; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 메시지 박스 서비스 - MessageBoxService /// <summary> /// 메시지 박스 서비스 /// </summary> protected IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } } #endregion #region 대화 상자 서비스 - DialogService /// <summary> /// 대화 상자 서비스 /// </summary> protected IDialogService DialogService { get { return GetService<IDialogService>(); } } #endregion #region 대화 상자 뷰 모델 - DialogViewModel /// <summary> /// 대화 상자 뷰 모델 /// </summary> protected DialogViewModel DialogViewModel { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainViewModel() /// <summary> /// 생성자 /// </summary> public MainViewModel() { DialogViewModel = new DialogViewModel(); ShowDialogCommand = new DelegateCommand(OnShowDialogCommandExecute); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Show Dialog 명령 실행시 처리하기 - OnShowDialogCommandExecute() /// <summary> /// Show Dialog 명령 실행시 처리하기 /// </summary> private void OnShowDialogCommandExecute() { UICommand command = DialogService.ShowDialog(DialogViewModel.DialogUICommandList, "User name", DialogViewModel); if(command != null) { MessageBoxService.Show("Dialog Clicked Button: " + command.Caption + ", "); } } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<Window x:Class="TestProject.MainWindow" 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:dxm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:local="clr-namespace:TestProject" Width="600" Height="450" Title="DialogService 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.DataContext> <local:MainViewModel /> </Window.DataContext> <dxm:Interaction.Behaviors> <dx:DXMessageBoxService /> <dx:DialogService DialogWindowStartupLocation="CenterOwner"> <dx:DialogService.ViewTemplate> <DataTemplate> <local:DialogView /> </DataTemplate> </dx:DialogService.ViewTemplate> <dx:DialogService.DialogStyle> <Style TargetType="dx:DXDialogWindow"> <Setter Property="SizeToContent" Value="WidthAndHeight" /> </Style> </dx:DialogService.DialogStyle> </dx:DialogService> </dxm:Interaction.Behaviors> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical"> <Button Margin="10" Padding="5" FontSize="16" Content="Show Dialog" Command="{Binding ShowDialogCommand}" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion } } |