11using System ;
22using System . Windows ;
33using System . Windows . Controls ;
4- using System . Windows . Media ;
54using System . Windows . Media . Animation ;
65using HandyControl . Data ;
76using HandyControl . Expression . Drawing ;
@@ -24,19 +23,32 @@ public class RunningBlock : ContentControl
2423
2524 public override void OnApplyTemplate ( )
2625 {
26+ if ( _elementPanel != null )
27+ {
28+ _elementPanel . SizeChanged -= ElementPanel_SizeChanged ;
29+ }
30+
2731 base . OnApplyTemplate ( ) ;
2832
2933 _elementContent = GetTemplateChild ( ElementContent ) as FrameworkElement ;
3034 _elementPanel = GetTemplateChild ( ElementPanel ) as Panel ;
35+
36+ if ( _elementPanel != null )
37+ {
38+ _elementPanel . SizeChanged += ElementPanel_SizeChanged ;
39+ }
40+
3141 UpdateContent ( ) ;
3242 }
3343
44+ private void ElementPanel_SizeChanged ( object sender , SizeChangedEventArgs e ) => UpdateContent ( ) ;
45+
3446 public static readonly DependencyProperty RunawayProperty = DependencyProperty . Register (
3547 "Runaway" , typeof ( bool ) , typeof ( RunningBlock ) , new FrameworkPropertyMetadata ( ValueBoxes . TrueBox , FrameworkPropertyMetadataOptions . AffectsRender ) ) ;
3648
3749 public bool Runaway
3850 {
39- get => ( bool ) GetValue ( RunawayProperty ) ;
51+ get => ( bool ) GetValue ( RunawayProperty ) ;
4052 set => SetValue ( RunawayProperty , ValueBoxes . BooleanBox ( value ) ) ;
4153 }
4254
@@ -45,7 +57,7 @@ public bool Runaway
4557
4658 public bool AutoRun
4759 {
48- get => ( bool ) GetValue ( AutoRunProperty ) ;
60+ get => ( bool ) GetValue ( AutoRunProperty ) ;
4961 set => SetValue ( AutoRunProperty , ValueBoxes . BooleanBox ( value ) ) ;
5062 }
5163
@@ -54,7 +66,7 @@ public bool AutoRun
5466
5567 public Orientation Orientation
5668 {
57- get => ( Orientation ) GetValue ( OrientationProperty ) ;
69+ get => ( Orientation ) GetValue ( OrientationProperty ) ;
5870 set => SetValue ( OrientationProperty , value ) ;
5971 }
6072
@@ -63,7 +75,7 @@ public Orientation Orientation
6375
6476 public Duration Duration
6577 {
66- get => ( Duration ) GetValue ( DurationProperty ) ;
78+ get => ( Duration ) GetValue ( DurationProperty ) ;
6779 set => SetValue ( DurationProperty , value ) ;
6880 }
6981
@@ -72,7 +84,7 @@ public Duration Duration
7284
7385 public double Speed
7486 {
75- get => ( double ) GetValue ( SpeedProperty ) ;
87+ get => ( double ) GetValue ( SpeedProperty ) ;
7688 set => SetValue ( SpeedProperty , value ) ;
7789 }
7890
@@ -93,7 +105,7 @@ public double Speed
93105
94106 public bool IsRunning
95107 {
96- get => ( bool ) GetValue ( IsRunningProperty ) ;
108+ get => ( bool ) GetValue ( IsRunningProperty ) ;
97109 set => SetValue ( IsRunningProperty , ValueBoxes . BooleanBox ( value ) ) ;
98110 }
99111
@@ -102,60 +114,57 @@ public bool IsRunning
102114
103115 public bool AutoReverse
104116 {
105- get => ( bool ) GetValue ( AutoReverseProperty ) ;
117+ get => ( bool ) GetValue ( AutoReverseProperty ) ;
106118 set => SetValue ( AutoReverseProperty , ValueBoxes . BooleanBox ( value ) ) ;
107119 }
108120
109121 private void UpdateContent ( )
110122 {
111123 if ( _elementContent == null || _elementPanel == null ) return ;
124+ if ( MathHelper . IsZero ( _elementPanel . ActualWidth ) || MathHelper . IsZero ( _elementPanel . ActualHeight ) ) return ;
112125
113126 _storyboard ? . Stop ( ) ;
114127
115- _elementPanel . Measure ( new Size ( double . PositiveInfinity , double . PositiveInfinity ) ) ;
116- _elementPanel . Width = _elementPanel . DesiredSize . Width ;
117- _elementPanel . Height = _elementPanel . DesiredSize . Height ;
118-
119128 double from ;
120129 double to ;
121130 PropertyPath propertyPath ;
122131
123132 if ( Orientation == Orientation . Horizontal )
124133 {
125- if ( AutoRun && _elementPanel . Width < ActualWidth )
134+ if ( AutoRun && _elementPanel . ActualWidth < ActualWidth )
126135 {
127136 return ;
128137 }
129138
130139 if ( Runaway )
131140 {
132- from = - _elementPanel . Width ;
141+ from = - _elementPanel . ActualWidth ;
133142 to = ActualWidth ;
134143 }
135144 else
136145 {
137146 from = 0 ;
138- to = ActualWidth - _elementPanel . Width ;
147+ to = ActualWidth - _elementPanel . ActualWidth ;
139148 SetCurrentValue ( AutoReverseProperty , ValueBoxes . TrueBox ) ;
140149 }
141150 propertyPath = new PropertyPath ( "(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)" ) ;
142151 }
143152 else
144153 {
145- if ( AutoRun && _elementPanel . Height < ActualHeight )
154+ if ( AutoRun && _elementPanel . ActualHeight < ActualHeight )
146155 {
147156 return ;
148157 }
149158
150159 if ( Runaway )
151160 {
152- from = - _elementPanel . Height ;
161+ from = - _elementPanel . ActualHeight ;
153162 to = ActualHeight ;
154163 }
155164 else
156165 {
157166 from = 0 ;
158- to = ActualHeight - _elementPanel . Height ;
167+ to = ActualHeight - _elementPanel . ActualHeight ;
159168 SetCurrentValue ( AutoReverseProperty , ValueBoxes . TrueBox ) ;
160169 }
161170 propertyPath = new PropertyPath ( "(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)" ) ;
@@ -180,7 +189,5 @@ private void UpdateContent()
180189 _storyboard . Children . Add ( animation ) ;
181190 _storyboard . Begin ( ) ;
182191 }
183-
184- protected override void OnRender ( DrawingContext drawingContext ) => UpdateContent ( ) ;
185192 }
186193}
0 commit comments