add PopupAdorner.StayOpen dp

This commit is contained in:
John Colvin committed 2019-08-14 00:24:34 -05:00
1 parent ae771f6927
commit 084e99325d
1 file changed
+37 -2
@@ -1,6 +1,10 @@
using bts.utils;
using bts;
using bts.utils;
using mbts.ui.extensions;
using mbts.utils;
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
@@ -11,7 +15,7 @@ namespace mbts.ui.controls.adorners {
class PopupAdorner : Adorner {
bool _enable_drag_move;
IDisposable _drag_move_hook;
IDisposable _drag_move_hook, _auto_close_hook;
public PopupAdorner(UIElement adornedElement, FrameworkElement popupTarget)
: this(adornedElement, popupTarget, null, HorizontalPosition.Middle, VerticalPosition.Bottom) { }
@@ -68,6 +72,37 @@ namespace mbts.ui.controls.adorners {
#endregion
#region StaysOpen
public static readonly DependencyProperty StaysOpenProperty =
Popup.StaysOpenProperty.AddOwner(typeof(PopupAdorner),
new FrameworkPropertyMetadata(
true,
(d, e) => {
var popa = ExceptionAssert.of_type<PopupAdorner>(d, nameof(d));
Disposable.dispose(ref popa._auto_close_hook);
if (!(bool)e.NewValue) {
FocusManager.SetIsFocusScope(popa.AdornedElement, true);
popa.AdornedElement.Focus();
popa.AdornedElement.PreviewGotKeyboardFocus += on_focus_changed;
popa.AdornedElement.GotFocus += on_focus_changed;
popa._auto_close_hook = Disposable.Create(() => {
popa.AdornedElement.PreviewGotKeyboardFocus -= on_focus_changed;
popa.AdornedElement.GotFocus -= on_focus_changed;
});
// callbacks
void on_focus_changed(object sender, RoutedEventArgs _e) {
if (!popa.PopupElement.IsKeyboardFocusWithin && FocusManager.GetFocusedElement(popa.PopupElement) == null)
popa.SetCurrentValue(IsOpenProperty, false);
};
}
}));
public bool StaysOpen {
get => (bool)GetValue(StaysOpenProperty);
set => SetValue(StaysOpenProperty, value);
}
#endregion
#region Opened Event
public static readonly RoutedEvent OpenedEvent = EventManager.RegisterRoutedEvent("Opened", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(PopupAdorner));