Skip to content

Commit

Permalink
fixed a bug of Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed May 18, 2020
1 parent a794c2a commit 2a89bf4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Background="{DynamicResource RegionBrush}"
MinWidth="500"
DataContext="{Binding DialogDemo,Source={StaticResource Locator}}">
<AdornerDecorator>
<hc:DialogContainer>
<hc:TransitioningContentControl>
<hc:SimplePanel>
<StackPanel Margin="32" VerticalAlignment="Center">
Expand All @@ -22,5 +22,5 @@
</StackPanel>
</hc:SimplePanel>
</hc:TransitioningContentControl>
</AdornerDecorator>
</hc:DialogContainer>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Dialog : ContentControl
{
private string _token;

private Adorner _container;
private AdornerContainer _container;

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

Expand Down Expand Up @@ -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<AdornerDecorator>(element);
}
else
{
ContainerDic.TryGetValue(token, out element);
decorator = element is System.Windows.Window ?
VisualHelper.GetChild<AdornerDecorator>(element) :
VisualHelper.GetChild<DialogContainer>(element);
}

if (element != null)
if (decorator != null)
{
var decorator = VisualHelper.GetChild<AdornerDecorator>(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);
}
}

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

private void Close(DependencyObject element)
{
if (element != null)
if (element != null && _container != null)
{
var decorator = VisualHelper.GetChild<AdornerDecorator>(element);
if (decorator != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Documents;

namespace HandyControl.Controls
{
public class DialogContainer : AdornerDecorator
{

}
}
3 changes: 2 additions & 1 deletion src/Shared/HandyControl_Shared/HandyControl_Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\FloatingBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Button\SplitButton.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Carousel\CarouselItem.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Dialog\DialogContainer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Growl\GrowlWindow.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Image\ImageBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Input\PinBox.cs" />
Expand Down Expand Up @@ -147,7 +148,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Carousel\Carousel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\ChatBubble.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\ColorPicker\ColorPicker.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Dialog.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Dialog\Dialog.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\GotoTop.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Gravatar.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Growl\Growl.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal static FrameworkElement GetImplementationRoot(DependencyObject d) =>

public static T GetChild<T>(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++)
Expand Down

0 comments on commit 2a89bf4

Please sign in to comment.