@@ -16,17 +16,17 @@ public class ButtonContentSnapBehavior : Behavior<ButtonBase>
1616{
1717 private const double DurationSeconds = 0.3 ;
1818
19- private bool attached ;
20- private long paddingChangedEventToken ;
21- private long contentChangedEventToken ;
22- private VisualStateGroup ? visualStateGroup ;
23- private ContentPresenter ? contentPresenter ;
24- private Visual ? contentVisual ;
25-
26- private Compositor compositor ;
27- private CompositionPropertySet propSet ;
28- private Vector3KeyFrameAnimation translationAnimation1 ;
29- private Vector3KeyFrameAnimation translationAnimation2 ;
19+ private bool _attached ;
20+ private long _paddingChangedEventToken ;
21+ private long _contentChangedEventToken ;
22+ private VisualStateGroup ? _visualStateGroup ;
23+ private ContentPresenter ? _contentPresenter ;
24+ private Visual ? _contentVisual ;
25+
26+ private Compositor _compositor ;
27+ private CompositionPropertySet _propSet ;
28+ private Vector3KeyFrameAnimation _translationAnimation1 ;
29+ private Vector3KeyFrameAnimation _translationAnimation2 ;
3030
3131
3232
@@ -47,22 +47,22 @@ public ButtonContentSnapType SnapType
4747
4848 private void AssociatedObject_Loaded ( object sender , RoutedEventArgs e )
4949 {
50- compositor = ElementCompositionPreview . GetElementVisual ( AssociatedObject ) . Compositor ;
50+ _compositor = ElementCompositionPreview . GetElementVisual ( AssociatedObject ) . Compositor ;
5151 //compositor = CompositionTarget.GetCompositorForCurrentThread();
5252
53- propSet = compositor . CreatePropertySet ( ) ;
54- propSet . InsertVector3 ( "Offset" , Vector3 . Zero ) ;
53+ _propSet = _compositor . CreatePropertySet ( ) ;
54+ _propSet . InsertVector3 ( "Offset" , Vector3 . Zero ) ;
5555
56- translationAnimation1 = compositor . CreateVector3KeyFrameAnimation ( ) ;
57- translationAnimation1 . InsertKeyFrame ( 1 , Vector3 . Zero ) ;
58- translationAnimation1 . Duration = TimeSpan . FromSeconds ( DurationSeconds ) ;
59- translationAnimation1 . StopBehavior = AnimationStopBehavior . LeaveCurrentValue ;
56+ _translationAnimation1 = _compositor . CreateVector3KeyFrameAnimation ( ) ;
57+ _translationAnimation1 . InsertKeyFrame ( 1 , Vector3 . Zero ) ;
58+ _translationAnimation1 . Duration = TimeSpan . FromSeconds ( DurationSeconds ) ;
59+ _translationAnimation1 . StopBehavior = AnimationStopBehavior . LeaveCurrentValue ;
6060
61- translationAnimation2 = compositor . CreateVector3KeyFrameAnimation ( ) ;
62- translationAnimation2 . InsertExpressionKeyFrame ( 1 , "propSet.Offset" ) ;
63- translationAnimation2 . Duration = TimeSpan . FromSeconds ( DurationSeconds ) ;
64- translationAnimation2 . SetReferenceParameter ( "propSet" , propSet ) ;
65- translationAnimation2 . StopBehavior = AnimationStopBehavior . LeaveCurrentValue ;
61+ _translationAnimation2 = _compositor . CreateVector3KeyFrameAnimation ( ) ;
62+ _translationAnimation2 . InsertExpressionKeyFrame ( 1 , "propSet.Offset" ) ;
63+ _translationAnimation2 . Duration = TimeSpan . FromSeconds ( DurationSeconds ) ;
64+ _translationAnimation2 . SetReferenceParameter ( "propSet" , _propSet ) ;
65+ _translationAnimation2 . StopBehavior = AnimationStopBehavior . LeaveCurrentValue ;
6666 TryLoadContent ( ( ButtonBase ) sender ) ;
6767 }
6868
@@ -101,66 +101,66 @@ private void TryLoadContent(ButtonBase? button)
101101
102102 private void LoadContent ( ButtonBase button )
103103 {
104- if ( attached ) return ;
104+ if ( _attached ) return ;
105105
106- paddingChangedEventToken = button . RegisterPropertyChangedCallback ( Control . PaddingProperty , OnPaddingPropertyChanged ) ;
107- visualStateGroup = VisualStateManager . GetVisualStateGroups ( ( FrameworkElement ) VisualTreeHelper . GetChild ( button , 0 ) ) . FirstOrDefault ( c => c . Name == "CommonStates" ) ;
106+ _paddingChangedEventToken = button . RegisterPropertyChangedCallback ( Control . PaddingProperty , OnPaddingPropertyChanged ) ;
107+ _visualStateGroup = VisualStateManager . GetVisualStateGroups ( ( FrameworkElement ) VisualTreeHelper . GetChild ( button , 0 ) ) . FirstOrDefault ( c => c . Name == "CommonStates" ) ;
108108
109- if ( visualStateGroup != null )
109+ if ( _visualStateGroup != null )
110110 {
111- visualStateGroup . CurrentStateChanging += VisualStateGroup_CurrentStateChanging ;
111+ _visualStateGroup . CurrentStateChanging += VisualStateGroup_CurrentStateChanging ;
112112 }
113113
114- contentPresenter = FindChild < ContentPresenter > ( button ) ;
114+ _contentPresenter = FindChild < ContentPresenter > ( button ) ;
115115
116- if ( contentPresenter != null )
116+ if ( _contentPresenter != null )
117117 {
118- contentChangedEventToken = contentPresenter . RegisterPropertyChangedCallback ( ContentPresenter . ContentProperty , OnContentChanged ) ;
119- var actualContent = VisualTreeHelper . GetChild ( contentPresenter , 0 ) as UIElement ;
118+ _contentChangedEventToken = _contentPresenter . RegisterPropertyChangedCallback ( ContentPresenter . ContentProperty , OnContentChanged ) ;
119+ var actualContent = VisualTreeHelper . GetChild ( _contentPresenter , 0 ) as UIElement ;
120120
121121 if ( actualContent != null )
122122 {
123- contentVisual = ElementCompositionPreview . GetElementVisual ( actualContent ) ;
124- contentVisual . IsPixelSnappingEnabled = true ;
123+ _contentVisual = ElementCompositionPreview . GetElementVisual ( actualContent ) ;
124+ _contentVisual . IsPixelSnappingEnabled = true ;
125125 ElementCompositionPreview . SetIsTranslationEnabled ( actualContent , true ) ;
126126 }
127127 }
128128
129- attached = true ;
129+ _attached = true ;
130130 UpdateSnapType ( ) ;
131131 }
132132
133133 private void UnloadContent ( ButtonBase button )
134134 {
135- if ( ! attached ) return ;
135+ if ( ! _attached ) return ;
136136
137- attached = false ;
137+ _attached = false ;
138138
139139 button . LayoutUpdated += AssociatedObject_LayoutUpdated ;
140140
141- button . UnregisterPropertyChangedCallback ( Control . PaddingProperty , paddingChangedEventToken ) ;
142- paddingChangedEventToken = 0 ;
141+ button . UnregisterPropertyChangedCallback ( Control . PaddingProperty , _paddingChangedEventToken ) ;
142+ _paddingChangedEventToken = 0 ;
143143
144- if ( visualStateGroup != null )
144+ if ( _visualStateGroup != null )
145145 {
146- visualStateGroup . CurrentStateChanging -= VisualStateGroup_CurrentStateChanging ;
146+ _visualStateGroup . CurrentStateChanging -= VisualStateGroup_CurrentStateChanging ;
147147 }
148- visualStateGroup = null ;
148+ _visualStateGroup = null ;
149149
150- if ( contentPresenter != null )
150+ if ( _contentPresenter != null )
151151 {
152- contentPresenter . UnregisterPropertyChangedCallback ( ContentPresenter . ContentProperty , contentChangedEventToken ) ;
153- contentChangedEventToken = 0 ;
154- contentPresenter = null ;
152+ _contentPresenter . UnregisterPropertyChangedCallback ( ContentPresenter . ContentProperty , _contentChangedEventToken ) ;
153+ _contentChangedEventToken = 0 ;
154+ _contentPresenter = null ;
155155 }
156156
157- if ( contentVisual != null )
157+ if ( _contentVisual != null )
158158 {
159- contentVisual . StopAnimation ( "Translation" ) ;
160- contentVisual = null ;
159+ _contentVisual . StopAnimation ( "Translation" ) ;
160+ _contentVisual = null ;
161161 }
162162
163- propSet . InsertVector3 ( "Offset" , Vector3 . Zero ) ;
163+ _propSet . InsertVector3 ( "Offset" , Vector3 . Zero ) ;
164164 }
165165
166166 protected override void OnAttached ( )
@@ -194,36 +194,36 @@ private void OnPaddingPropertyChanged(DependencyObject sender, DependencyPropert
194194
195195 private void OnContentChanged ( DependencyObject sender , DependencyProperty dp )
196196 {
197- if ( attached && contentPresenter != null )
197+ if ( _attached && _contentPresenter != null )
198198 {
199- if ( contentVisual != null )
199+ if ( _contentVisual != null )
200200 {
201- contentVisual . StopAnimation ( "Translation" ) ;
202- contentVisual = null ;
201+ _contentVisual . StopAnimation ( "Translation" ) ;
202+ _contentVisual = null ;
203203 }
204204
205- var actualContent = VisualTreeHelper . GetChild ( contentPresenter , 0 ) as UIElement ;
205+ var actualContent = VisualTreeHelper . GetChild ( _contentPresenter , 0 ) as UIElement ;
206206
207207 if ( actualContent != null )
208208 {
209- contentVisual = ElementCompositionPreview . GetElementVisual ( actualContent ) ;
210- contentVisual . IsPixelSnappingEnabled = true ;
209+ _contentVisual = ElementCompositionPreview . GetElementVisual ( actualContent ) ;
210+ _contentVisual . IsPixelSnappingEnabled = true ;
211211 ElementCompositionPreview . SetIsTranslationEnabled ( actualContent , true ) ;
212212 }
213213 }
214214 }
215215
216216 private void VisualStateGroup_CurrentStateChanging ( object sender , VisualStateChangedEventArgs e )
217217 {
218- if ( ! attached || contentVisual == null ) return ;
218+ if ( ! _attached || _contentVisual == null ) return ;
219219
220220 if ( e . NewState ? . Name == "PointerOver" || e . NewState ? . Name == "Pressed" )
221221 {
222- contentVisual . StartAnimation ( "Translation" , translationAnimation1 ) ;
222+ _contentVisual . StartAnimation ( "Translation" , _translationAnimation1 ) ;
223223 }
224224 else
225225 {
226- contentVisual . StartAnimation ( "Translation" , translationAnimation2 ) ;
226+ _contentVisual . StartAnimation ( "Translation" , _translationAnimation2 ) ;
227227 }
228228 }
229229
@@ -233,14 +233,14 @@ private void UpdateSnapType()
233233 var button = AssociatedObject ;
234234 if ( button == null ) return ;
235235
236- if ( attached )
236+ if ( _attached )
237237 {
238238 var hover = false ;
239239
240- if ( visualStateGroup != null )
240+ if ( _visualStateGroup != null )
241241 {
242- hover = visualStateGroup . CurrentState ? . Name == "PointerOver"
243- || visualStateGroup . CurrentState ? . Name == "Pressed" ;
242+ hover = _visualStateGroup . CurrentState ? . Name == "PointerOver"
243+ || _visualStateGroup . CurrentState ? . Name == "Pressed" ;
244244 }
245245
246246 var padding = button . Padding ;
@@ -254,19 +254,19 @@ private void UpdateSnapType()
254254 _ => Vector3 . Zero
255255 } ;
256256
257- propSet . InsertVector3 ( "Offset" , offset ) ;
257+ _propSet . InsertVector3 ( "Offset" , offset ) ;
258258
259- if ( contentVisual != null )
259+ if ( _contentVisual != null )
260260 {
261- contentVisual . StopAnimation ( "Translation" ) ;
261+ _contentVisual . StopAnimation ( "Translation" ) ;
262262
263263 if ( hover )
264264 {
265- contentVisual . Properties . InsertVector3 ( "Translation" , Vector3 . Zero ) ;
265+ _contentVisual . Properties . InsertVector3 ( "Translation" , Vector3 . Zero ) ;
266266 }
267267 else
268268 {
269- contentVisual . Properties . InsertVector3 ( "Translation" , offset ) ;
269+ _contentVisual . Properties . InsertVector3 ( "Translation" , offset ) ;
270270 }
271271 }
272272 }
0 commit comments