From e2527c870bb0ec9badaf83ae14bb3a8dfb8d70d9 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 8 Mar 2011 23:22:52 +0000 Subject: [PATCH] MessageBox: Refactored. Also implemented the DefaultResult so you can now specify which button to use as the default button. --- .../MessageBox/Implementation/MessageBox.cs | 208 ++++++++++++------ .../MessageBox/Themes/Generic.xaml | 75 ++++--- 2 files changed, 180 insertions(+), 103 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Implementation/MessageBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Implementation/MessageBox.cs index 729325ca..ccc163e6 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Implementation/MessageBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Implementation/MessageBox.cs @@ -19,6 +19,11 @@ namespace Microsoft.Windows.Controls /// private MessageBoxButton _button = MessageBoxButton.OK; + /// + /// Tracks the MessageBoxResult to set as the default and focused button + /// + private MessageBoxResult _defaultResult = MessageBoxResult.None; + #endregion //Private Members #region Constructors @@ -129,47 +134,15 @@ namespace Microsoft.Windows.Controls { base.OnApplyTemplate(); - DragWidget = (Thumb)GetTemplateChild("PART_DragWidget"); + DragWidget = GetTemplateChild("PART_DragWidget") as Thumb; if (DragWidget != null) DragWidget.DragDelta += (o, e) => ProcessMove(e); - CloseButton = (Button)GetTemplateChild("PART_CloseButton"); - if (CloseButton != null) - CloseButton.Click += (o, e) => Close(); - - NoButton = (Button)GetTemplateChild("PART_NoButton"); - if (NoButton != null) - NoButton.Click += (o, e) => Button_Click(o, e); - - NoButton1 = (Button)GetTemplateChild("PART_NoButton1"); - if (NoButton1 != null) - NoButton1.Click += (o, e) => Button_Click(o, e); - - YesButton = (Button)GetTemplateChild("PART_YesButton"); - if (YesButton != null) - YesButton.Click += (o, e) => Button_Click(o, e); - - YesButton1 = (Button)GetTemplateChild("PART_YesButton1"); - if (YesButton1 != null) - YesButton1.Click += (o, e) => Button_Click(o, e); - - CancelButton = (Button)GetTemplateChild("PART_CancelButton"); - if (CancelButton != null) - CancelButton.Click += (o, e) => Button_Click(o, e); - - CancelButton1 = (Button)GetTemplateChild("PART_CancelButton1"); - if (CancelButton1 != null) - CancelButton1.Click += (o, e) => Button_Click(o, e); - - OkButton = (Button)GetTemplateChild("PART_OkButton"); - if (OkButton != null) - OkButton.Click += (o, e) => Button_Click(o, e); - - OkButton1 = (Button)GetTemplateChild("PART_OkButton1"); - if (OkButton1 != null) - OkButton1.Click += (o, e) => Button_Click(o, e); + AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(Button_Click)); ChangeVisualState(_button.ToString(), true); + + SetDefaultResult(); } #endregion //Base Class Overrides @@ -208,7 +181,7 @@ namespace Microsoft.Windows.Controls /// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user. public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button) { - return ShowCore(messageText, caption, button, MessageBoxImage.None); + return ShowCore(messageText, caption, button, MessageBoxImage.None, MessageBoxResult.None); } /// @@ -221,48 +194,29 @@ namespace Microsoft.Windows.Controls /// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user. public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon) { - return ShowCore(messageText, caption, button, icon); - } - - #endregion //Public Static - - #region Private Static - - private static MessageBoxResult ShowCore(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon) - { - MessageBox msgBox = new MessageBox(); - msgBox.InitializeMessageBox(messageText, caption, button, icon); - msgBox.Show(); - return msgBox.MessageBoxResult; + return ShowCore(messageText, caption, button, icon, MessageBoxResult.None); } /// - /// Resolves the owner Window of the MessageBox. + /// Displays a message box that has a message and that returns a result. /// - /// - private static FrameworkElement ResolveOwner() + /// A System.String that specifies the text to display. + /// A System.String that specifies the title bar caption to display. + /// A System.Windows.MessageBoxButton value that specifies which button or buttons to display. + /// A System.Windows.MessageBoxImage value that specifies the icon to display. + /// A System.Windows.MessageBoxResult value that specifies the default result of the MessageBox. + /// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user. + public static MessageBoxResult Show(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) { - FrameworkElement owner = null; - if (Application.Current != null) - { - foreach (Window w in Application.Current.Windows) - { - if (w.IsActive) - { - owner = w; - break; - } - } - } - return owner; + return ShowCore(messageText, caption, button, icon, defaultResult); } - #endregion //Private Static + #endregion //Public Static #region Protected /// - /// Shows the MessageBox + /// Shows the container which contains the MessageBox. /// protected void Show() { @@ -276,11 +230,12 @@ namespace Microsoft.Windows.Controls /// The caption. /// The button. /// The image. - protected void InitializeMessageBox(string text, string caption, MessageBoxButton button, MessageBoxImage image) + protected void InitializeMessageBox(string text, string caption, MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult) { Text = text; Caption = caption; _button = button; + _defaultResult = defaultResult; SetImageSource(image); Container = CreateContainer(); } @@ -299,6 +254,118 @@ namespace Microsoft.Windows.Controls #region Private + /// + /// Sets the button that represents the _defaultResult to the default button and gives it focus. + /// + private void SetDefaultResult() + { + var defaultButton = GetDefaultButtonFromDefaultResult(); + if (defaultButton != null) + { + defaultButton.IsDefault = true; + defaultButton.Focus(); + } + } + + /// + /// Gets the default button from the _defaultResult. + /// + /// The default button that represents the defaultResult + private Button GetDefaultButtonFromDefaultResult() + { + Button defaultButton = null; + switch (_defaultResult) + { + case MessageBoxResult.Cancel: + defaultButton = GetMessageBoxButton("PART_CancelButton"); + break; + case MessageBoxResult.No: + defaultButton = GetMessageBoxButton("PART_NoButton"); + break; + case MessageBoxResult.OK: + defaultButton = GetMessageBoxButton("PART_OkButton"); + break; + case MessageBoxResult.Yes: + defaultButton = GetMessageBoxButton("PART_YesButton"); + break; + case MessageBoxResult.None: + defaultButton = GetDefaultButton(); + break; + } + return defaultButton; + } + + /// + /// Gets the default button. + /// + /// Used when the _defaultResult is set to None + /// The button to use as the default + private Button GetDefaultButton() + { + Button defaultButton = null; + switch (_button) + { + case MessageBoxButton.OK: + case MessageBoxButton.OKCancel: + defaultButton = GetMessageBoxButton("PART_OkButton"); + break; + case MessageBoxButton.YesNo: + case MessageBoxButton.YesNoCancel: + defaultButton = GetMessageBoxButton("PART_YesButton"); + break; + } + return defaultButton; + } + + /// + /// Gets a message box button. + /// + /// The name of the button to get. + /// The button + private Button GetMessageBoxButton(string name) + { + Button button = GetTemplateChild(name) as Button; + return button; + } + + /// + /// Shows the MessageBox. + /// + /// The message text. + /// The caption. + /// The button. + /// The icon. + /// The default result. + /// + private static MessageBoxResult ShowCore(string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) + { + MessageBox msgBox = new MessageBox(); + msgBox.InitializeMessageBox(messageText, caption, button, icon, defaultResult); + msgBox.Show(); + return msgBox.MessageBoxResult; + } + + /// + /// Resolves the owner Window of the MessageBox. + /// + /// the owner element + private static FrameworkElement ResolveOwner() + { + FrameworkElement owner = null; + if (Application.Current != null) + { + foreach (Window w in Application.Current.Windows) + { + if (w.IsActive) + { + owner = w; + break; + } + } + } + return owner; + } + /// /// Sets the message image source. /// @@ -389,7 +456,7 @@ namespace Microsoft.Windows.Controls /// The instance containing the event data. private void Button_Click(object sender, RoutedEventArgs e) { - Button button = e.Source as Button; + Button button = e.OriginalSource as Button; switch (button.Name) { case "PART_NoButton": @@ -400,6 +467,7 @@ namespace Microsoft.Windows.Controls case "PART_YesButton1": MessageBoxResult = MessageBoxResult.Yes; break; + case "PART_CloseButton": case "PART_CancelButton": case "PART_CancelButton1": MessageBoxResult = MessageBoxResult.Cancel; diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Themes/Generic.xaml index 82f0ba77..efbd5084 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MessageBox/Themes/Generic.xaml @@ -122,7 +122,7 @@ - + Visible @@ -133,7 +133,14 @@ - + + + + Visible + + + + Visible @@ -144,7 +151,14 @@ - + + + + Visible + + + + Visible @@ -156,7 +170,21 @@ - + + + + Visible + + + + + + + Visible + + + + Visible @@ -222,35 +250,16 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + +