From 30575922fe60ce5bd412bfdf5c5e773455547de9 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Sun, 24 Mar 2019 01:14:07 +0800
Subject: [PATCH] fixed a bug of messagebox
---
HandyControl/Controls/Window/MessageBox.cs | 95 +++++++-------
HandyControl/Controls/Window/Window.cs | 93 +++++---------
HandyControl/Themes/Styles/MessageBox.xaml | 120 ++++++++++++++----
HandyControl/Themes/Styles/Window.xaml | 15 ++-
.../Resources/Themes/Styles/Style.xaml | 27 +---
5 files changed, 189 insertions(+), 161 deletions(-)
diff --git a/HandyControl/Controls/Window/MessageBox.cs b/HandyControl/Controls/Window/MessageBox.cs
index 36007596..c6c6cd85 100644
--- a/HandyControl/Controls/Window/MessageBox.cs
+++ b/HandyControl/Controls/Window/MessageBox.cs
@@ -1,14 +1,10 @@
-using System;
-using System.ComponentModel;
+using System.ComponentModel;
using System.Windows;
-using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
-using System.Windows.Shapes;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
-using HandyControl.Tools.Extension;
namespace HandyControl.Controls
{
@@ -17,14 +13,6 @@ namespace HandyControl.Controls
///
public sealed class MessageBox : Window
{
- #region Constants
-
- private const string ElementImage = "PART_Image";
-
- private const string ElementMessage = "PART_Message";
-
- #endregion Constants
-
private MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel;
private bool _showOk;
@@ -35,13 +23,41 @@ namespace HandyControl.Controls
private bool _showNo;
- private string _message;
+ public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
+ "Message", typeof(string), typeof(MessageBox), new PropertyMetadata(default(string)));
- private Geometry _image;
+ public string Message
+ {
+ get => (string) GetValue(MessageProperty);
+ set => SetValue(MessageProperty, value);
+ }
- private Brush _imageBrush;
+ public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
+ "Image", typeof(Geometry), typeof(MessageBox), new PropertyMetadata(default(Geometry)));
- private bool _showImage;
+ public Geometry Image
+ {
+ get => (Geometry) GetValue(ImageProperty);
+ set => SetValue(ImageProperty, value);
+ }
+
+ public static readonly DependencyProperty ImageBrushProperty = DependencyProperty.Register(
+ "ImageBrush", typeof(Brush), typeof(MessageBox), new PropertyMetadata(default(Brush)));
+
+ public Brush ImageBrush
+ {
+ get => (Brush) GetValue(ImageBrushProperty);
+ set => SetValue(ImageBrushProperty, value);
+ }
+
+ public static readonly DependencyProperty ShowImageProperty = DependencyProperty.Register(
+ "ShowImage", typeof(bool), typeof(MessageBox), new PropertyMetadata(ValueBoxes.FalseBox));
+
+ public bool ShowImage
+ {
+ get => (bool) GetValue(ShowImageProperty);
+ set => SetValue(ShowImageProperty, value);
+ }
private MessageBox()
{
@@ -67,23 +83,6 @@ namespace HandyControl.Controls
}, (s, e) => e.CanExecute = _showNo));
}
- protected override void OnSourceInitialized(EventArgs e)
- {
- base.OnSourceInitialized(e);
-
- if (LogicalTreeHelper.FindLogicalNode(this, ElementMessage) is TextBlock message) message.Text = _message;
-
- if (_showImage)
- {
- if (LogicalTreeHelper.FindLogicalNode(this, ElementImage) is Path image)
- {
- image.Data = _image;
- image.Fill = _imageBrush;
- image.Show();
- }
- }
- }
-
///
/// 成功
///
@@ -96,9 +95,9 @@ namespace HandyControl.Controls
{
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
SetButtonStatus(messageBox, MessageBoxButton.OK);
- messageBox._showImage = true;
- messageBox._image = ResourceHelper.GetResource(ResourceToken.SuccessGeometry);
- messageBox._imageBrush = ResourceHelper.GetResource(ResourceToken.SuccessBrush);
+ messageBox.ShowImage = true;
+ messageBox.Image = ResourceHelper.GetResource(ResourceToken.SuccessGeometry);
+ messageBox.ImageBrush = ResourceHelper.GetResource(ResourceToken.SuccessBrush);
messageBox.ShowDialog();
});
@@ -174,9 +173,9 @@ namespace HandyControl.Controls
{
messageBox = CreateMessageBox(null, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
SetButtonStatus(messageBox, MessageBoxButton.OK);
- messageBox._showImage = true;
- messageBox._image = ResourceHelper.GetResource(ResourceToken.FatalGeometry);
- messageBox._imageBrush = ResourceHelper.GetResource(ResourceToken.PrimaryTextBrush);
+ messageBox.ShowImage = true;
+ messageBox.Image = ResourceHelper.GetResource(ResourceToken.FatalGeometry);
+ messageBox.ImageBrush = ResourceHelper.GetResource(ResourceToken.PrimaryTextBrush);
messageBox.ShowDialog();
});
@@ -217,9 +216,9 @@ namespace HandyControl.Controls
if (!string.IsNullOrEmpty(info.IconKey))
{
- messageBox._showImage = true;
- messageBox._image = ResourceHelper.GetResource(info.IconKey);
- messageBox._imageBrush = ResourceHelper.GetResource(info.IconBrushKey);
+ messageBox.ShowImage = true;
+ messageBox.Image = ResourceHelper.GetResource(info.IconKey);
+ messageBox.ImageBrush = ResourceHelper.GetResource(info.IconBrushKey);
}
messageBox.Style = info.Style;
@@ -293,9 +292,9 @@ namespace HandyControl.Controls
return new MessageBox
{
- _message = messageBoxText,
+ Message = messageBoxText,
Owner = ownerWindow,
- WindowStartupLocation = ownerIsNull ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
+ WindowStartupLocation = WindowStartupLocation.CenterScreen,
ShowTitle = true,
Title = caption ?? string.Empty,
Topmost = ownerIsNull,
@@ -352,9 +351,9 @@ namespace HandyControl.Controls
}
if (string.IsNullOrEmpty(iconKey)) return;
- messageBox._showImage = true;
- messageBox._image = ResourceHelper.GetResource(iconKey);
- messageBox._imageBrush = ResourceHelper.GetResource(iconBrushKey);
+ messageBox.ShowImage = true;
+ messageBox.Image = ResourceHelper.GetResource(iconKey);
+ messageBox.ImageBrush = ResourceHelper.GetResource(iconBrushKey);
}
private static bool IsValidMessageBoxButton(MessageBoxButton value)
diff --git a/HandyControl/Controls/Window/Window.cs b/HandyControl/Controls/Window/Window.cs
index ee8d9fc1..be713cfb 100644
--- a/HandyControl/Controls/Window/Window.cs
+++ b/HandyControl/Controls/Window/Window.cs
@@ -1,11 +1,9 @@
using System;
using System.Windows;
-using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Data;
-using HandyControl.Tools;
using HandyControl.Tools.Extension;
#if netle40
using Microsoft.Windows.Shell;
@@ -81,14 +79,14 @@ namespace HandyControl.Controls
private bool _isFullScreen;
+ private Thickness _actualBorderThickness;
+
private UIElement _nonClientArea;
private bool _showNonClientArea = true;
private double _tempNonClientAreaHeight;
- private Thickness _tempThickness;
-
private WindowState _tempWindowState;
private WindowStyle _tempWindowStyle;
@@ -106,31 +104,7 @@ namespace HandyControl.Controls
new Binding(NonClientAreaHeightProperty.Name) {Source = this});
WindowChrome.SetWindowChrome(this, chrome);
- Loaded += delegate
- {
- _tempThickness = BorderThickness;
- if (WindowState == WindowState.Maximized)
- {
- BorderThickness = new Thickness();
- }
-
- CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand,
- (s, e) => WindowState = WindowState.Minimized));
- CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand,
- (s, e) => WindowState = WindowState.Maximized));
- CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand,
- (s, e) => WindowState = WindowState.Normal));
- CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, (s, e) => Close()));
- CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu));
-
- _tempNonClientAreaHeight = NonClientAreaHeight;
- _tempWindowState = WindowState;
- _tempWindowStyle = WindowStyle;
- _tempResizeMode = ResizeMode;
-
- SwitchIsFullScreen(_isFullScreen);
- SwitchShowNonClientArea(_showNonClientArea);
- };
+ Loaded += (s, e) => OnLoaded(e);
}
public Brush CloseButtonBackground
@@ -317,15 +291,34 @@ namespace HandyControl.Controls
}
else if (WindowState == WindowState.Normal)
{
- BorderThickness = _tempThickness;
+ BorderThickness = _actualBorderThickness;
}
}
- public override void OnApplyTemplate()
+ protected void OnLoaded(RoutedEventArgs args)
{
- base.OnApplyTemplate();
+ _actualBorderThickness = BorderThickness;
+ if (WindowState == WindowState.Maximized)
+ {
+ BorderThickness = new Thickness();
+ }
- _nonClientArea = GetTemplateChild(ElementNonClientArea) as UIElement;
+ CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand,
+ (s, e) => WindowState = WindowState.Minimized));
+ CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand,
+ (s, e) => WindowState = WindowState.Maximized));
+ CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand,
+ (s, e) => WindowState = WindowState.Normal));
+ CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, (s, e) => Close()));
+ CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu));
+
+ _tempNonClientAreaHeight = NonClientAreaHeight;
+ _tempWindowState = WindowState;
+ _tempWindowStyle = WindowStyle;
+ _tempResizeMode = ResizeMode;
+
+ SwitchIsFullScreen(_isFullScreen);
+ SwitchShowNonClientArea(_showNonClientArea);
if (SizeToContent != SizeToContent.WidthAndHeight)
return;
@@ -334,11 +327,18 @@ namespace HandyControl.Controls
Dispatcher.BeginInvoke(new Action(() => { SizeToContent = SizeToContent.WidthAndHeight; }));
}
+ public override void OnApplyTemplate()
+ {
+ base.OnApplyTemplate();
+
+ _nonClientArea = GetTemplateChild(ElementNonClientArea) as UIElement;
+ }
+
private void ShowSystemMenu(object sender, ExecutedRoutedEventArgs e)
{
var point = WindowState == WindowState.Maximized
- ? new Point(0, 28)
- : new Point(Left, Top + 28);
+ ? new Point(0, NonClientAreaHeight)
+ : new Point(Left, Top + NonClientAreaHeight);
SystemCommands.ShowSystemMenu(this, point);
}
@@ -348,28 +348,5 @@ namespace HandyControl.Controls
if (SizeToContent == SizeToContent.WidthAndHeight)
InvalidateMeasure();
}
-
- ///
- /// 获取自定义窗口
- ///
- ///
- public static Window GetCustomWindow(FrameworkElement content)
- {
- var window = new Window
- {
- Style = ResourceHelper.GetResource
-
\ No newline at end of file
diff --git a/HandyControl/Themes/Styles/Window.xaml b/HandyControl/Themes/Styles/Window.xaml
index c31413e4..c93bc3f5 100644
--- a/HandyControl/Themes/Styles/Window.xaml
+++ b/HandyControl/Themes/Styles/Window.xaml
@@ -46,25 +46,26 @@
-
-
\ No newline at end of file