예제 #1
0
        /// <summary>
        /// 대화상자 표시
        /// </summary>
        /// <param name="pageContext">페이지 컨텍스트</param>
        /// <returns>대화상자 결과</returns>
        public Task <bool?> ShowDialog(PageContext pageContext)
        {
            var owner = pageContext.Owner?.View is DependencyObject dependencyObject?Window.GetWindow(dependencyObject) : Application.Current.Windows.OfType <Window>().SingleOrDefault(w => w.IsActive) ?? Application.Current.MainWindow;

            if (owner?.IsLoaded != true)
            {
                owner = null;
            }

            if (!(pageContext.View is Window dialogWindow))
            {
                dialogWindow = new ThemeWindow
                {
                    SizeToContent = SizeToContent.WidthAndHeight
                };

                dialogWindow.SetBinding(Window.TitleProperty, nameof(pageContext.Title));
                dialogWindow.SetBinding(ContentControl.ContentProperty, nameof(pageContext.View));
                dialogWindow.SetBinding(Window.ResizeModeProperty, new Binding {
                    Path = new PropertyPath($"{nameof(pageContext.View)}.(0)", ResizeModeProperty)
                });
                dialogWindow.SetBinding(Window.ShowInTaskbarProperty, new Binding {
                    Path = new PropertyPath($"{nameof(pageContext.View)}.(0)", ShowInTaskbarProperty)
                });

                if (owner != null)
                {
                    dialogWindow.SetBinding(Window.IconProperty, new Binding(nameof(owner.Icon))
                    {
                        Source = owner
                    });
                }

                (pageContext.View as FrameworkElement)?.SetBinding(FrameworkElement.DataContextProperty, nameof(pageContext.ViewModel));
                (pageContext.View as FrameworkContentElement)?.SetBinding(FrameworkContentElement.DataContextProperty, nameof(pageContext.ViewModel));

                dialogWindow.DataContext = pageContext;
            }
예제 #2
0
        /// <summary>
        /// 메시지 상자 표시
        /// </summary>
        /// <param name="owner">소유자 창</param>
        /// <param name="messageBoxText">메시지</param>
        /// <param name="caption">제목</param>
        /// <param name="button">메시지 상자에 표시되는 단추</param>
        /// <param name="icon">아이콘</param>
        /// <param name="defaultResult">기본 선택 메시지 상자 결과</param>
        /// <returns>메시지 상자 결과</returns>
        public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            if (owner == null)
            {
                owner = Application.Current.Windows.OfType <Window>().SingleOrDefault(w => w.IsActive) ?? Application.Current.MainWindow;
            }

            if (owner?.IsLoaded != true)
            {
                owner = null;
            }

            var content = new ThemeMessageBoxContentControl
            {
                Content = messageBoxText,
                button  = button,
                Icon    = icon,
                result  = defaultResult,
            };

            var messageBoxWindow = new ThemeWindow()
            {
                ResizeMode            = ResizeMode.NoResize,
                Owner                 = owner,
                ShowInTaskbar         = owner == null,
                WindowStartupLocation = owner == null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
                SizeToContent         = SizeToContent.WidthAndHeight,
                Title                 = caption,
                Content               = content
            };

            if (owner != null)
            {
                messageBoxWindow.SetBinding(Window.IconProperty, new Binding(nameof(owner.Icon))
                {
                    Source = owner
                });
            }

            content.window = messageBoxWindow;

            switch (icon)
            {
            case MessageBoxImage.Hand:
                SystemSounds.Hand.Play();
                break;

            case MessageBoxImage.Asterisk:
                SystemSounds.Asterisk.Play();
                break;

            case MessageBoxImage.Question:
                SystemSounds.Question.Play();
                break;

            case MessageBoxImage.Warning:
                SystemSounds.Exclamation.Play();
                break;
            }

            messageBoxWindow.KeyDown += (s, e) =>
            {
                if (e.Key == System.Windows.Input.Key.Escape)
                {
                    switch (button)
                    {
                    case MessageBoxButton.OK:
                        content.result = MessageBoxResult.OK;
                        break;

                    case MessageBoxButton.OKCancel:
                    case MessageBoxButton.YesNo:
                    case MessageBoxButton.YesNoCancel:
                        content.result = MessageBoxResult.Cancel;
                        break;
                    }
                    messageBoxWindow.Close();
                }
            };

            messageBoxWindow.ShowDialog();
            return(content.result);
        }