Skip to content

Commit 2a89bf4

Browse files
committed
fixed a bug of Dialog
1 parent a794c2a commit 2a89bf4

5 files changed

Lines changed: 34 additions & 22 deletions

File tree

src/Shared/HandyControlDemo_Shared/UserControl/Controls/DialogDemoCtl.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Background="{DynamicResource RegionBrush}"
1010
MinWidth="500"
1111
DataContext="{Binding DialogDemo,Source={StaticResource Locator}}">
12-
<AdornerDecorator>
12+
<hc:DialogContainer>
1313
<hc:TransitioningContentControl>
1414
<hc:SimplePanel>
1515
<StackPanel Margin="32" VerticalAlignment="Center">
@@ -22,5 +22,5 @@
2222
</StackPanel>
2323
</hc:SimplePanel>
2424
</hc:TransitioningContentControl>
25-
</AdornerDecorator>
25+
</hc:DialogContainer>
2626
</UserControl>

src/Shared/HandyControl_Shared/Controls/Other/Dialog.cs renamed to src/Shared/HandyControl_Shared/Controls/Dialog/Dialog.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Dialog : ContentControl
1414
{
1515
private string _token;
1616

17-
private Adorner _container;
17+
private AdornerContainer _container;
1818

1919
private static readonly Dictionary<string, FrameworkElement> ContainerDic = new Dictionary<string, FrameworkElement>();
2020

@@ -107,35 +107,36 @@ public static Dialog Show(object content, string token = "")
107107

108108
FrameworkElement element;
109109

110+
AdornerDecorator decorator;
110111
if (string.IsNullOrEmpty(token))
111112
{
112113
element = WindowHelper.GetActiveWindow();
114+
decorator = VisualHelper.GetChild<AdornerDecorator>(element);
113115
}
114116
else
115117
{
116118
ContainerDic.TryGetValue(token, out element);
119+
decorator = element is System.Windows.Window ?
120+
VisualHelper.GetChild<AdornerDecorator>(element) :
121+
VisualHelper.GetChild<DialogContainer>(element);
117122
}
118123

119-
if (element != null)
124+
if (decorator != null)
120125
{
121-
var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
122-
if (decorator != null)
126+
if (decorator.Child != null)
123127
{
124-
if (decorator.Child != null)
125-
{
126-
decorator.Child.IsEnabled = false;
127-
}
128-
var layer = decorator.AdornerLayer;
129-
if (layer != null)
128+
decorator.Child.IsEnabled = false;
129+
}
130+
var layer = decorator.AdornerLayer;
131+
if (layer != null)
132+
{
133+
var container = new AdornerContainer(layer)
130134
{
131-
var container = new AdornerContainer(layer)
132-
{
133-
Child = dialog
134-
};
135-
dialog._container = container;
136-
dialog.IsClosed = false;
137-
layer.Add(container);
138-
}
135+
Child = dialog
136+
};
137+
dialog._container = container;
138+
dialog.IsClosed = false;
139+
layer.Add(container);
139140
}
140141
}
141142

@@ -156,7 +157,7 @@ public void Close()
156157

157158
private void Close(DependencyObject element)
158159
{
159-
if (element != null)
160+
if (element != null && _container != null)
160161
{
161162
var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
162163
if (decorator != null)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Windows.Documents;
2+
3+
namespace HandyControl.Controls
4+
{
5+
public class DialogContainer : AdornerDecorator
6+
{
7+
8+
}
9+
}

src/Shared/HandyControl_Shared/HandyControl_Shared.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\FloatingBlock.cs" />
2828
<Compile Include="$(MSBuildThisFileDirectory)Controls\Button\SplitButton.cs" />
2929
<Compile Include="$(MSBuildThisFileDirectory)Controls\Carousel\CarouselItem.cs" />
30+
<Compile Include="$(MSBuildThisFileDirectory)Controls\Dialog\DialogContainer.cs" />
3031
<Compile Include="$(MSBuildThisFileDirectory)Controls\Growl\GrowlWindow.cs" />
3132
<Compile Include="$(MSBuildThisFileDirectory)Controls\Image\ImageBlock.cs" />
3233
<Compile Include="$(MSBuildThisFileDirectory)Controls\Input\PinBox.cs" />
@@ -147,7 +148,7 @@
147148
<Compile Include="$(MSBuildThisFileDirectory)Controls\Carousel\Carousel.cs" />
148149
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\ChatBubble.cs" />
149150
<Compile Include="$(MSBuildThisFileDirectory)Controls\ColorPicker\ColorPicker.cs" />
150-
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Dialog.cs" />
151+
<Compile Include="$(MSBuildThisFileDirectory)Controls\Dialog\Dialog.cs" />
151152
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\GotoTop.cs" />
152153
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Gravatar.cs" />
153154
<Compile Include="$(MSBuildThisFileDirectory)Controls\Growl\Growl.cs" />

src/Shared/HandyControl_Shared/Tools/Helper/VisualHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal static FrameworkElement GetImplementationRoot(DependencyObject d) =>
2727

2828
public static T GetChild<T>(DependencyObject d) where T : DependencyObject
2929
{
30+
if (d == null) return default;
3031
if (d is T t) return t;
3132

3233
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)

0 commit comments

Comments
 (0)