@@ -70,6 +70,7 @@ public enum PopupBoxPopupMode
70
70
/// </summary>
71
71
[ TemplatePart ( Name = PopupPartName , Type = typeof ( Popup ) ) ]
72
72
[ TemplatePart ( Name = PopupContentControlPartName , Type = typeof ( ContentControl ) ) ]
73
+ [ TemplatePart ( Name = TogglePartName , Type = typeof ( ToggleButton ) ) ]
73
74
[ TemplateVisualState ( GroupName = "PopupStates" , Name = PopupIsOpenStateName ) ]
74
75
[ TemplateVisualState ( GroupName = "PopupStates" , Name = PopupIsClosedStateName ) ]
75
76
[ ContentProperty ( "PopupContent" ) ]
@@ -82,6 +83,7 @@ public class PopupBox : ContentControl
82
83
public const string PopupIsClosedStateName = "IsClosed" ;
83
84
private PopupEx _popup ;
84
85
private ContentControl _popupContentControl ;
86
+ private ToggleButton _toggleButton ;
85
87
86
88
static PopupBox ( )
87
89
{
@@ -166,10 +168,8 @@ private static void IsPopupOpenPropertyChangedCallback(DependencyObject dependen
166
168
Mouse . Capture ( null ) ;
167
169
}
168
170
169
- if ( newValue )
170
- {
171
- popupBox . AnimateChildren ( ) ;
172
- }
171
+ if ( newValue )
172
+ popupBox . AnimateChildren ( ) ;
173
173
174
174
VisualStateManager . GoToState ( popupBox , newValue ? PopupIsOpenStateName : PopupIsClosedStateName , true ) ;
175
175
}
@@ -228,14 +228,19 @@ public override void OnApplyTemplate()
228
228
{
229
229
if ( _popup != null )
230
230
_popup . Loaded -= PopupOnLoaded ;
231
+ if ( _toggleButton != null )
232
+ _toggleButton . PreviewMouseLeftButtonUp -= ToggleButtonOnPreviewMouseLeftButtonUp ;
231
233
232
234
base . OnApplyTemplate ( ) ;
233
235
234
236
_popup = GetTemplateChild ( PopupPartName ) as PopupEx ;
235
237
_popupContentControl = GetTemplateChild ( PopupContentControlPartName ) as ContentControl ;
238
+ _toggleButton = GetTemplateChild ( TogglePartName ) as ToggleButton ;
236
239
237
240
if ( _popup != null )
238
241
_popup . Loaded += PopupOnLoaded ;
242
+ if ( _toggleButton != null )
243
+ _toggleButton . PreviewMouseLeftButtonUp += ToggleButtonOnPreviewMouseLeftButtonUp ;
239
244
240
245
VisualStateManager . GoToState ( this , IsPopupOpen ? PopupIsOpenStateName : PopupIsClosedStateName , false ) ;
241
246
}
@@ -254,12 +259,12 @@ protected override void OnMouseEnter(MouseEventArgs e)
254
259
{
255
260
if ( PopupMode == PopupBoxPopupMode . MouseOverEager
256
261
|| PopupMode == PopupBoxPopupMode . MouseOver )
257
-
262
+
258
263
SetCurrentValue ( IsPopupOpenProperty , true ) ;
259
264
260
265
base . OnMouseEnter ( e ) ;
261
266
}
262
-
267
+
263
268
protected override void OnMouseLeave ( MouseEventArgs e )
264
269
{
265
270
if ( PopupMode == PopupBoxPopupMode . MouseOverEager
@@ -419,7 +424,7 @@ private static void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
419
424
}
420
425
421
426
protected override void OnMouseLeftButtonUp ( MouseButtonEventArgs e )
422
- {
427
+ {
423
428
if ( IsPopupOpen && ! StaysOpen )
424
429
{
425
430
Close ( ) ;
@@ -437,6 +442,15 @@ private void PopupOnLoaded(object sender, RoutedEventArgs routedEventArgs)
437
442
_popup . IsOpen = true ;
438
443
}
439
444
445
+ private void ToggleButtonOnPreviewMouseLeftButtonUp ( object sender , MouseButtonEventArgs mouseButtonEventArgs )
446
+ {
447
+ if ( PopupMode == PopupBoxPopupMode . Click || ! IsPopupOpen ) return ;
448
+
449
+ Close ( ) ;
450
+ Mouse . Capture ( null ) ;
451
+ mouseButtonEventArgs . Handled = true ;
452
+ }
453
+
440
454
private static object CoerceToolTipIsEnabled ( DependencyObject dependencyObject , object value )
441
455
{
442
456
var popupBox = ( PopupBox ) dependencyObject ;
0 commit comments