@@ -17,23 +17,14 @@ namespace HandyControl.Controls;
1717public class Drawer : FrameworkElement
1818{
1919 private Storyboard _storyboard ;
20-
2120 private AdornerContainer _container ;
22-
2321 private ContentControl _animationControl ;
24-
2522 private TranslateTransform _translateTransform ;
26-
2723 private double _animationLength ;
28-
2924 private string _animationPropertyName ;
30-
3125 private FrameworkElement _maskElement ;
32-
3326 private AdornerLayer _layer ;
34-
3527 private UIElement _contentElement ;
36-
3728 private Point _contentRenderTransformOrigin ;
3829
3930 static Drawer ( )
@@ -59,7 +50,8 @@ private void Drawer_Loaded(object sender, RoutedEventArgs e)
5950 private static void DataContextPropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e ) =>
6051 ( ( Drawer ) d ) . OnDataContextPropertyChanged ( e ) ;
6152
62- private void OnDataContextPropertyChanged ( DependencyPropertyChangedEventArgs e ) => UpdateDataContext ( _animationControl , e . OldValue , e . NewValue ) ;
53+ private void OnDataContextPropertyChanged ( DependencyPropertyChangedEventArgs e ) =>
54+ UpdateDataContext ( _animationControl , e . OldValue , e . NewValue ) ;
6355
6456 public static readonly RoutedEvent OpenedEvent =
6557 EventManager . RegisterRoutedEvent ( "Opened" , RoutingStrategy . Bubble ,
@@ -82,7 +74,9 @@ public event RoutedEventHandler Closed
8274 }
8375
8476 public static readonly DependencyProperty IsOpenProperty = DependencyProperty . Register (
85- nameof ( IsOpen ) , typeof ( bool ) , typeof ( Drawer ) , new FrameworkPropertyMetadata ( ValueBoxes . FalseBox , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , OnIsOpenChanged ) ) ;
77+ nameof ( IsOpen ) , typeof ( bool ) , typeof ( Drawer ) ,
78+ new FrameworkPropertyMetadata ( ValueBoxes . FalseBox , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault ,
79+ OnIsOpenChanged ) ) ;
8680
8781 private static void OnIsOpenChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
8882 {
@@ -218,7 +212,8 @@ private void CreateContainer()
218212
219213 _animationControl . DataContext = DataContext ;
220214 _animationControl . CommandBindings . Clear ( ) ;
221- _animationControl . CommandBindings . Add ( new CommandBinding ( ControlCommands . Close , ( s , e ) => SetCurrentValue ( IsOpenProperty , ValueBoxes . FalseBox ) ) ) ;
215+ _animationControl . CommandBindings . Add ( new CommandBinding ( ControlCommands . Close ,
216+ ( s , e ) => SetCurrentValue ( IsOpenProperty , ValueBoxes . FalseBox ) ) ) ;
222217 panel . Children . Add ( _animationControl ) ;
223218 _container = new AdornerContainer ( _layer )
224219 {
@@ -248,31 +243,20 @@ private void OnIsOpenChanged(bool isOpen)
248243 return ;
249244 }
250245
251- AdornerDecorator decorator ;
252- var parent = VisualHelper . GetParent < DrawerContainer > ( this ) ;
253- if ( parent ! = null )
246+ AdornerDecorator decorator = VisualHelper . GetParent < DrawerContainer > ( this ) ;
247+ decorator ?? = VisualHelper . GetChild < AdornerDecorator > ( System . Windows . Window . GetWindow ( this ) ) ;
248+ if ( decorator = = null )
254249 {
255- _contentElement = parent . Child ;
256- decorator = parent ;
257- }
258- else
259- {
260- var window = WindowHelper . GetActiveWindow ( ) ;
261- if ( window == null )
262- {
263- return ;
264- }
265-
266- decorator = VisualHelper . GetChild < AdornerDecorator > ( window ) ;
267- _contentElement = window . Content as UIElement ;
250+ return ;
268251 }
269252
253+ _contentElement = decorator . Child ;
270254 if ( _contentElement == null )
271255 {
272256 return ;
273257 }
274258
275- _layer = decorator ? . AdornerLayer ;
259+ _layer = decorator . AdornerLayer ;
276260 if ( _layer == null )
277261 {
278262 return ;
@@ -328,6 +312,7 @@ private void OnIsOpenChanged(bool isOpen)
328312 Storyboard . SetTargetProperty ( drawerAnimation , new PropertyPath ( _animationPropertyName ) ) ;
329313 _storyboard . Children . Add ( drawerAnimation ) ;
330314 }
315+
331316 _storyboard . Begin ( ) ;
332317 }
333318
@@ -380,15 +365,17 @@ private void ShowByPress(bool isOpen)
380365 ? AnimationHelper . CreateAnimation ( .9 )
381366 : AnimationHelper . CreateAnimation ( 1 ) ;
382367 Storyboard . SetTarget ( animationX , _contentElement ) ;
383- Storyboard . SetTargetProperty ( animationX , new PropertyPath ( "(UIElement.RenderTransform).(ScaleTransform.ScaleX)" ) ) ;
368+ Storyboard . SetTargetProperty ( animationX ,
369+ new PropertyPath ( "(UIElement.RenderTransform).(ScaleTransform.ScaleX)" ) ) ;
384370
385371 _storyboard . Children . Add ( animationX ) ;
386372
387373 var animationY = isOpen
388374 ? AnimationHelper . CreateAnimation ( .9 )
389375 : AnimationHelper . CreateAnimation ( 1 ) ;
390376 Storyboard . SetTarget ( animationY , _contentElement ) ;
391- Storyboard . SetTargetProperty ( animationY , new PropertyPath ( "(UIElement.RenderTransform).(ScaleTransform.ScaleY)" ) ) ;
377+ Storyboard . SetTargetProperty ( animationY ,
378+ new PropertyPath ( "(UIElement.RenderTransform).(ScaleTransform.ScaleY)" ) ) ;
392379
393380 _storyboard . Children . Add ( animationY ) ;
394381 }
0 commit comments