@@ -35,13 +35,18 @@ public enum ClockDisplayAutomation
35
35
[ TemplatePart ( Name = MinutesCanvasPartName , Type = typeof ( Canvas ) ) ]
36
36
[ TemplatePart ( Name = MinuteReadOutPartName , Type = typeof ( TextBlock ) ) ]
37
37
[ TemplatePart ( Name = HourReadOutPartName , Type = typeof ( TextBlock ) ) ]
38
+ [ TemplateVisualState ( GroupName = "DisplayModeStates" , Name = HoursVisualStateName ) ]
39
+ [ TemplateVisualState ( GroupName = "DisplayModeStates" , Name = MinutesVisualStateName ) ]
38
40
public class Clock : Control
39
41
{
40
42
public const string HoursCanvasPartName = "PART_HoursCanvas" ;
41
43
public const string MinutesCanvasPartName = "PART_MinutesCanvas" ;
42
44
public const string MinuteReadOutPartName = "PART_MinuteReadOut" ;
43
45
public const string HourReadOutPartName = "PART_HourReadOut" ;
44
46
47
+ public const string HoursVisualStateName = "Hours" ;
48
+ public const string MinutesVisualStateName = "Minutes" ;
49
+
45
50
private Point _centreCanvas = new Point ( 0 , 0 ) ;
46
51
private Point _currentStartPosition = new Point ( 0 , 0 ) ;
47
52
private TextBlock _hourReadOutPartName ;
@@ -131,9 +136,14 @@ public bool Is24Hours
131
136
132
137
133
138
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty . Register (
134
- "DisplayMode" , typeof ( ClockDisplayMode ) , typeof ( Clock ) , new PropertyMetadata ( ClockDisplayMode . Hours ) ) ;
139
+ "DisplayMode" , typeof ( ClockDisplayMode ) , typeof ( Clock ) , new FrameworkPropertyMetadata ( ClockDisplayMode . Hours , DisplayModePropertyChangedCallback ) ) ;
140
+
141
+ private static void DisplayModePropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
142
+ {
143
+ ( ( Clock ) dependencyObject ) . GotoVisualState ( ! TransitionAssist . GetDisableTransitions ( dependencyObject ) ) ;
144
+ }
135
145
136
- public ClockDisplayMode DisplayMode
146
+ public ClockDisplayMode DisplayMode
137
147
{
138
148
get { return ( ClockDisplayMode ) GetValue ( DisplayModeProperty ) ; }
139
149
set { SetValue ( DisplayModeProperty , value ) ; }
@@ -235,8 +245,16 @@ public override void OnApplyTemplate()
235
245
_minuteReadOutPartName . PreviewMouseLeftButtonDown += MinuteReadOutPartNameOnPreviewMouseLeftButtonDown ;
236
246
237
247
base . OnApplyTemplate ( ) ;
248
+
249
+ GotoVisualState ( false ) ;
238
250
}
239
251
252
+ private void GotoVisualState ( bool useTransitions )
253
+ {
254
+ VisualStateManager . GoToState ( this ,
255
+ DisplayMode == ClockDisplayMode . Minutes ? MinutesVisualStateName : HoursVisualStateName , useTransitions ) ;
256
+ }
257
+
240
258
private void GenerateButtons ( )
241
259
{
242
260
var hoursCanvas = GetTemplateChild ( HoursCanvasPartName ) as Canvas ;
0 commit comments