diff --git a/samples/ControlCatalog/ControlCatalog.csproj b/samples/ControlCatalog/ControlCatalog.csproj index 862de9d320..4fb0ff2059 100644 --- a/samples/ControlCatalog/ControlCatalog.csproj +++ b/samples/ControlCatalog/ControlCatalog.csproj @@ -23,6 +23,8 @@ + + @@ -84,6 +86,7 @@ Designer + Designer @@ -158,6 +161,7 @@ MenuPage.xaml + ProgressBarPage.xaml diff --git a/samples/ControlCatalog/MainView.xaml b/samples/ControlCatalog/MainView.xaml index a0e0df450b..3a19d8e8ed 100644 --- a/samples/ControlCatalog/MainView.xaml +++ b/samples/ControlCatalog/MainView.xaml @@ -27,5 +27,6 @@ + diff --git a/samples/ControlCatalog/Pages/PopupPage.xaml b/samples/ControlCatalog/Pages/PopupPage.xaml new file mode 100644 index 0000000000..8469ac480d --- /dev/null +++ b/samples/ControlCatalog/Pages/PopupPage.xaml @@ -0,0 +1,49 @@ + + + Popup + A Popup dialog + + Flyout Auto close + Flyout Toggle + + + + + Item A + Item B + Item C + Item D + Item E + Item F + Item G + + Nested Auto close + + + + Nested Content + + + + + + + + + + + Item A + Item B + Item C + Item D + Item E + Item F + Item G + + + + + + + \ No newline at end of file diff --git a/samples/ControlCatalog/Pages/PopupPage.xaml.cs b/samples/ControlCatalog/Pages/PopupPage.xaml.cs new file mode 100644 index 0000000000..08aca56bad --- /dev/null +++ b/samples/ControlCatalog/Pages/PopupPage.xaml.cs @@ -0,0 +1,19 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace ControlCatalog.Pages +{ + public class PopupPage : UserControl + { + public PopupPage() + { + this.InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +} diff --git a/src/Avalonia.Controls/Primitives/Popup.cs b/src/Avalonia.Controls/Primitives/Popup.cs index 5cd3b22fc9..b8817d28f3 100644 --- a/src/Avalonia.Controls/Primitives/Popup.cs +++ b/src/Avalonia.Controls/Primitives/Popup.cs @@ -215,7 +215,17 @@ namespace Avalonia.Controls.Primitives { var window = _topLevel as Window; if (window != null) - window.Deactivated += WindowDeactivated; + { + window.Deactivated += WindowDeactivated; + } + else + { + var parentPopuproot = _topLevel as PopupRoot; + if(parentPopuproot != null && parentPopuproot.Parent!=null) + { + ((Popup)(parentPopuproot.Parent)).Closed += ParentClosed; + } + } _topLevel.AddHandler(PointerPressedEvent, PointerPressedOutside, RoutingStrategies.Tunnel); _nonClientListener = InputManager.Instance.Process.Subscribe(ListenForNonClientClick); } @@ -230,7 +240,7 @@ namespace Avalonia.Controls.Primitives Opened?.Invoke(this, EventArgs.Empty); } - + /// /// Closes the popup. /// @@ -244,6 +254,14 @@ namespace Avalonia.Controls.Primitives var window = _topLevel as Window; if (window != null) window.Deactivated -= WindowDeactivated; + else + { + var parentPopuproot = _topLevel as PopupRoot; + if (parentPopuproot != null && parentPopuproot.Parent != null) + { + ((Popup)parentPopuproot.Parent).Closed -= ParentClosed; + } + } _nonClientListener?.Dispose(); _nonClientListener = null; } @@ -381,16 +399,25 @@ namespace Avalonia.Controls.Primitives { if (!StaysOpen) { - var root = ((IVisual)e.Source).GetVisualRoot(); - - if (root != this.PopupRoot) - { + if(!IsChildOrThis((IVisual)e.Source)) + { Close(); e.Handled = true; } } } + private bool IsChildOrThis(IVisual child) + { + IVisual root = child.GetVisualRoot(); + while (root is PopupRoot) + { + if (root == PopupRoot) return true; + root = ((PopupRoot)root).Parent.GetVisualRoot(); + } + return false; + } + private void WindowDeactivated(object sender, EventArgs e) { if (!StaysOpen) @@ -398,5 +425,13 @@ namespace Avalonia.Controls.Primitives Close(); } } + + private void ParentClosed(object sender, EventArgs e) + { + if (!StaysOpen) + { + Close(); + } + } } } \ No newline at end of file