1
- using System ;
1
+ using ControlzEx ;
2
+ using System ;
2
3
using System . ComponentModel ;
3
- using System . Diagnostics ;
4
4
using System . Linq ;
5
5
using System . Runtime . InteropServices ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
8
6
using System . Windows ;
9
7
using System . Windows . Controls ;
10
8
using System . Windows . Controls . Primitives ;
11
9
using System . Windows . Input ;
12
10
using System . Windows . Markup ;
13
11
using System . Windows . Media ;
14
12
using System . Windows . Media . Animation ;
15
- using ControlzEx ;
16
13
17
14
namespace MaterialDesignThemes . Wpf
18
15
{
@@ -110,13 +107,19 @@ public class PopupBox : ContentControl
110
107
private ContentControl _popupContentControl ;
111
108
private ToggleButton _toggleButton ;
112
109
private Point _popupPointFromLastRequest ;
110
+ private Point _lastRelativePositon ;
113
111
114
112
static PopupBox ( )
115
113
{
116
114
DefaultStyleKeyProperty . OverrideMetadata ( typeof ( PopupBox ) , new FrameworkPropertyMetadata ( typeof ( PopupBox ) ) ) ;
117
115
ToolTipService . IsEnabledProperty . OverrideMetadata ( typeof ( PopupBox ) , new FrameworkPropertyMetadata ( null , CoerceToolTipIsEnabled ) ) ;
118
116
EventManager . RegisterClassHandler ( typeof ( PopupBox ) , Mouse . LostMouseCaptureEvent , new MouseEventHandler ( OnLostMouseCapture ) ) ;
119
- EventManager . RegisterClassHandler ( typeof ( PopupBox ) , Mouse . MouseDownEvent , new MouseButtonEventHandler ( OnMouseButtonDown ) , true ) ;
117
+ EventManager . RegisterClassHandler ( typeof ( PopupBox ) , Mouse . MouseDownEvent , new MouseButtonEventHandler ( OnMouseButtonDown ) , true ) ;
118
+ }
119
+
120
+ public PopupBox ( )
121
+ {
122
+ LayoutUpdated += OnLayoutUpdated ;
120
123
}
121
124
122
125
public static readonly DependencyProperty ToggleContentProperty = DependencyProperty . Register (
@@ -229,8 +232,8 @@ private static void IsPopupOpenPropertyChangedCallback(DependencyObject dependen
229
232
else
230
233
Mouse . Capture ( null ) ;
231
234
}
232
-
233
- popupBox . AnimateChildrenIn ( ! newValue ) ;
235
+
236
+ popupBox . AnimateChildrenIn ( ! newValue ) ;
234
237
popupBox . _popup ? . RefreshPosition ( ) ;
235
238
236
239
VisualStateManager . GoToState ( popupBox , newValue ? PopupIsOpenStateName : PopupIsClosedStateName , true ) ;
@@ -239,7 +242,6 @@ private static void IsPopupOpenPropertyChangedCallback(DependencyObject dependen
239
242
popupBox . OnOpened ( ) ;
240
243
else
241
244
popupBox . OnClosed ( ) ;
242
-
243
245
}
244
246
245
247
/// <summary>
@@ -398,7 +400,7 @@ public override void OnApplyTemplate()
398
400
if ( _popup != null )
399
401
_popup . Loaded += PopupOnLoaded ;
400
402
if ( _toggleButton != null )
401
- _toggleButton . PreviewMouseLeftButtonUp += ToggleButtonOnPreviewMouseLeftButtonUp ;
403
+ _toggleButton . PreviewMouseLeftButtonUp += ToggleButtonOnPreviewMouseLeftButtonUp ;
402
404
403
405
VisualStateManager . GoToState ( this , IsPopupOpen ? PopupIsOpenStateName : PopupIsClosedStateName , false ) ;
404
406
}
@@ -408,7 +410,7 @@ protected override void OnIsKeyboardFocusWithinChanged(DependencyPropertyChanged
408
410
base . OnIsKeyboardFocusWithinChanged ( e ) ;
409
411
410
412
if ( IsPopupOpen && ! IsKeyboardFocusWithin )
411
- {
413
+ {
412
414
Close ( ) ;
413
415
}
414
416
}
@@ -439,9 +441,22 @@ protected override void OnMouseEnter(MouseEventArgs e)
439
441
440
442
SetCurrentValue ( IsPopupOpenProperty , true ) ;
441
443
}
442
-
443
444
base . OnMouseEnter ( e ) ;
444
445
}
446
+
447
+ private void OnLayoutUpdated ( object sender , EventArgs eventArgs )
448
+ {
449
+ if ( _popupContentControl != null && _popup != null &&
450
+ ( PopupMode == PopupBoxPopupMode . MouseOver || PopupMode == PopupBoxPopupMode . MouseOverEager ) )
451
+ {
452
+ Point relativePosition = _popupContentControl . TranslatePoint ( new Point ( ) , this ) ;
453
+ if ( relativePosition != _lastRelativePositon )
454
+ {
455
+ _popup . RefreshPosition ( ) ;
456
+ _lastRelativePositon = _popupContentControl . TranslatePoint ( new Point ( ) , this ) ;
457
+ }
458
+ }
459
+ }
445
460
446
461
protected override void OnMouseLeave ( MouseEventArgs e )
447
462
{
@@ -461,16 +476,16 @@ protected void Close()
461
476
462
477
private CustomPopupPlacement [ ] GetPopupPlacement ( Size popupSize , Size targetSize , Point offset )
463
478
{
464
- double x , y ;
465
-
479
+ double x , y ;
480
+
466
481
if ( FlowDirection == FlowDirection . RightToLeft )
467
482
offset . X += targetSize . Width / 2 ;
468
-
483
+
469
484
switch ( PlacementMode )
470
485
{
471
486
case PopupBoxPlacementMode . BottomAndAlignLeftEdges :
472
487
x = 0 - Math . Abs ( offset . X * 3 ) ;
473
- y = targetSize . Height - Math . Abs ( offset . Y ) ;
488
+ y = targetSize . Height - Math . Abs ( offset . Y ) ;
474
489
break ;
475
490
case PopupBoxPlacementMode . BottomAndAlignRightEdges :
476
491
x = 0 - popupSize . Width + targetSize . Width - offset . X ;
@@ -699,7 +714,7 @@ private static void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
699
714
}
700
715
701
716
protected override void OnMouseLeftButtonUp ( MouseButtonEventArgs e )
702
- {
717
+ {
703
718
if ( IsPopupOpen && ! StaysOpen )
704
719
{
705
720
Close ( ) ;
@@ -735,7 +750,7 @@ private void ToggleButtonOnPreviewMouseLeftButtonUp(object sender, MouseButtonEv
735
750
736
751
Close ( ) ;
737
752
Mouse . Capture ( null ) ;
738
- mouseButtonEventArgs . Handled = true ;
753
+ mouseButtonEventArgs . Handled = true ;
739
754
}
740
755
741
756
private static object CoerceToolTipIsEnabled ( DependencyObject dependencyObject , object value )
0 commit comments