첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ DialogViewModel.cs

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;

using DevExpress.Xpf.Mvvm;

namespace HowToUseDialogService
{
    /// <summary>
    /// 대화 상자 뷰 모델
    /// </summary>
    public class DialogViewModel : ViewModelBase
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 사용자명
        /// </summary>
        private string userName;

        /// <summary>
        /// 대화 상자 닫기 가능 여부
        /// </summary>
        private bool canCloseDialog = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 사용자명 - UserName

        /// <summary>
        /// 사용자명
        /// </summary>
        public string UserName
        {
            get
            {
                return this.userName;
            }
            set
            {
                SetProperty(ref this.userName, value, () => UserName);
            }
        }

        #endregion

        #region 대화 상자 닫기 가능 여부 - CanCloseDialog

        /// <summary>
        /// 대화 상자 닫기 가능 여부
        /// </summary>
        public bool CanCloseDialog
        {
            get
            {
                return this.canCloseDialog;
            }
            set
            {
                SetProperty(ref this.canCloseDialog, 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="pCancelEventArgs">이벤트 인자</param>
        private void e(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
    }
}

 

728x90

 

▶ DialogView.xaml

<UserControl
    x:Class="HowToUseDialogService.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>

 

300x250

 

▶ MainViewModel.cs

using System.Windows;
using System.Windows.Input;

using DevExpress.Xpf.Mvvm;

namespace HowToUseDialogService
{
    /// <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 uiCommand = DialogService.ShowDialog(DialogViewModel.DialogUICommandList, "User name", DialogViewModel);

            if(uiCommand != null)
            {
                MessageBoxService.Show("Dialog Clicked Button: " + uiCommand.Caption + ", ");
            }
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window
    x:Class="HowToUseDialogService.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:HowToUseDialogService"
    Title="DialogService"
    Width="600"
    Height="450">
    <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>

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요