From 2a89bf42f738cd2148594066d173becd2f8e0c57 Mon Sep 17 00:00:00 2001
From: NaBian <836904362@qq.com>
Date: Mon, 18 May 2020 23:04:35 +0800
Subject: [PATCH] fixed a bug of Dialog
---
.../UserControl/Controls/DialogDemoCtl.xaml | 4 +-
.../Controls/{Other => Dialog}/Dialog.cs | 39 ++++++++++---------
.../Controls/Dialog/DialogContainer.cs | 9 +++++
.../HandyControl_Shared.projitems | 3 +-
.../Tools/Helper/VisualHelper.cs | 1 +
5 files changed, 34 insertions(+), 22 deletions(-)
rename src/Shared/HandyControl_Shared/Controls/{Other => Dialog}/Dialog.cs (83%)
create mode 100644 src/Shared/HandyControl_Shared/Controls/Dialog/DialogContainer.cs
diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DialogDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DialogDemoCtl.xaml
index 2c99969a2..3df8f88e6 100644
--- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DialogDemoCtl.xaml
+++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/DialogDemoCtl.xaml
@@ -9,7 +9,7 @@
Background="{DynamicResource RegionBrush}"
MinWidth="500"
DataContext="{Binding DialogDemo,Source={StaticResource Locator}}">
-
+
@@ -22,5 +22,5 @@
-
+
diff --git a/src/Shared/HandyControl_Shared/Controls/Other/Dialog.cs b/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
similarity index 83%
rename from src/Shared/HandyControl_Shared/Controls/Other/Dialog.cs
rename to src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
index 31c93d2e0..0161ea094 100644
--- a/src/Shared/HandyControl_Shared/Controls/Other/Dialog.cs
+++ b/src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs
@@ -14,7 +14,7 @@ public class Dialog : ContentControl
{
private string _token;
- private Adorner _container;
+ private AdornerContainer _container;
private static readonly Dictionary ContainerDic = new Dictionary();
@@ -107,35 +107,36 @@ public static Dialog Show(object content, string token = "")
FrameworkElement element;
+ AdornerDecorator decorator;
if (string.IsNullOrEmpty(token))
{
element = WindowHelper.GetActiveWindow();
+ decorator = VisualHelper.GetChild(element);
}
else
{
ContainerDic.TryGetValue(token, out element);
+ decorator = element is System.Windows.Window ?
+ VisualHelper.GetChild(element) :
+ VisualHelper.GetChild(element);
}
- if (element != null)
+ if (decorator != null)
{
- var decorator = VisualHelper.GetChild(element);
- if (decorator != null)
+ if (decorator.Child != null)
{
- if (decorator.Child != null)
- {
- decorator.Child.IsEnabled = false;
- }
- var layer = decorator.AdornerLayer;
- if (layer != null)
+ decorator.Child.IsEnabled = false;
+ }
+ var layer = decorator.AdornerLayer;
+ if (layer != null)
+ {
+ var container = new AdornerContainer(layer)
{
- var container = new AdornerContainer(layer)
- {
- Child = dialog
- };
- dialog._container = container;
- dialog.IsClosed = false;
- layer.Add(container);
- }
+ Child = dialog
+ };
+ dialog._container = container;
+ dialog.IsClosed = false;
+ layer.Add(container);
}
}
@@ -156,7 +157,7 @@ public void Close()
private void Close(DependencyObject element)
{
- if (element != null)
+ if (element != null && _container != null)
{
var decorator = VisualHelper.GetChild(element);
if (decorator != null)
diff --git a/src/Shared/HandyControl_Shared/Controls/Dialog/DialogContainer.cs b/src/Shared/HandyControl_Shared/Controls/Dialog/DialogContainer.cs
new file mode 100644
index 000000000..539bf07f4
--- /dev/null
+++ b/src/Shared/HandyControl_Shared/Controls/Dialog/DialogContainer.cs
@@ -0,0 +1,9 @@
+using System.Windows.Documents;
+
+namespace HandyControl.Controls
+{
+ public class DialogContainer : AdornerDecorator
+ {
+
+ }
+}
\ No newline at end of file
diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems
index 076eb50bd..f1268f178 100644
--- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems
+++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems
@@ -27,6 +27,7 @@
+
@@ -147,7 +148,7 @@
-
+
diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/VisualHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/VisualHelper.cs
index 84a088e17..5fd55877e 100644
--- a/src/Shared/HandyControl_Shared/Tools/Helper/VisualHelper.cs
+++ b/src/Shared/HandyControl_Shared/Tools/Helper/VisualHelper.cs
@@ -27,6 +27,7 @@ internal static FrameworkElement GetImplementationRoot(DependencyObject d) =>
public static T GetChild(DependencyObject d) where T : DependencyObject
{
+ if (d == null) return default;
if (d is T t) return t;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)