private void OnButton1Click(object sender, RoutedEventArgs e) { switch (button) { case MessageBoxButton.OK: case MessageBoxButton.OKCancel: result = MessageBoxResult.OK; window.Close(); break; case MessageBoxButton.YesNo: case MessageBoxButton.YesNoCancel: result = MessageBoxResult.Yes; window.Close(); break; } }
/// <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); }