1
// -------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All Rights Reserved.
3 // -------------------------------------------------------------------
6 using System
.Collections
;
7 using System
.Collections
.Generic
;
8 using System
.Collections
.ObjectModel
;
10 using System
.Windows
.Controls
;
11 using System
.Windows
.Markup
;
12 using System
.Windows
.Media
.Animation
;
16 namespace System
.Windows
19 /// A group of mutually exclusive visual states.
21 [ContentProperty("States")]
22 public class VisualStateGroup
: DependencyObject
25 /// The Name of the VisualStateGroup.
30 internal set { _name = value; }
34 /// VisualStates in the group.
42 _states
= new Collection
<VisualState
>();
50 /// Transitions between VisualStates in the group.
52 public IList Transitions
56 if (_transitions
== null)
58 _transitions
= new Collection
<VisualTransition
>();
66 /// VisualState that is currently applied.
68 internal VisualState CurrentState
73 internal VisualState
GetState(string stateName
)
75 for (int stateIndex
= 0; stateIndex
< States
.Count
; ++stateIndex
)
77 VisualState state
= (VisualState
)States
[stateIndex
];
78 if (state
.Name
== stateName
)
87 internal Collection
<Storyboard
> CurrentStoryboards
91 if (_currentStoryboards
== null)
93 _currentStoryboards
= new Collection
<Storyboard
>();
96 return _currentStoryboards
;
100 internal void StartNewThenStopOld(FrameworkElement element
, params Storyboard
[] newStoryboards
)
102 // Start the new Storyboards
103 for (int index
= 0; index
< newStoryboards
.Length
; ++index
)
105 if (newStoryboards
[index
] == null)
110 element
.Resources
.Add (newStoryboards
[index
].native
.ToString (), newStoryboards
[index
]);
112 newStoryboards
[index
].Begin();
114 // If an exception is thrown calling begin, clear all the SBs out of the tree before propagating
115 for (int i
= 0; i
<= index
; i
++)
116 if (newStoryboards
[i
] != null)
117 element
.Resources
.Remove (newStoryboards
[i
].native
.ToString ());
121 // Silverlight had an issue where initially, a checked CheckBox would not show the check mark
122 // until the second frame. They chose to do a Seek(0) at this point, which this line
123 // is supposed to mimic. It does not seem to be equivalent, though, and WPF ends up
124 // with some odd animation behavior. I haven't seen the CheckBox issue on WPF, so
125 // commenting this out for now.
126 // newStoryboards[index].SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime);
129 // Stop the old Storyboards
130 for (int index
= 0; index
< CurrentStoryboards
.Count
; ++index
)
132 if (CurrentStoryboards
[index
] == null)
137 element
.Resources
.Remove (CurrentStoryboards
[index
].native
.ToString ());
138 CurrentStoryboards
[index
].Stop();
141 // Hold on to the running Storyboards
142 CurrentStoryboards
.Clear();
143 for (int index
= 0; index
< newStoryboards
.Length
; ++index
)
145 CurrentStoryboards
.Add(newStoryboards
[index
]);
149 internal void RaiseCurrentStateChanging(FrameworkElement element
, VisualState oldState
, VisualState newState
, Control control
)
151 RaiseCurrentStateChanging (new VisualStateChangedEventArgs(oldState
, newState
, control
));
154 internal void RaiseCurrentStateChanged(FrameworkElement element
, VisualState oldState
, VisualState newState
, Control control
)
156 RaiseCurrentStateChanged(new VisualStateChangedEventArgs(oldState
, newState
, control
));
159 internal void RaiseCurrentStateChanging (VisualStateChangedEventArgs e
)
161 EventHandler
<VisualStateChangedEventArgs
> h
= (EventHandler
<VisualStateChangedEventArgs
>) EventList
[CurrentStateChangingEvent
];
166 internal void RaiseCurrentStateChanged (VisualStateChangedEventArgs e
)
168 EventHandler
<VisualStateChangedEventArgs
> h
= (EventHandler
<VisualStateChangedEventArgs
>) EventList
[CurrentStateChangedEvent
];
173 static object CurrentStateChangingEvent
= new object ();
174 static object CurrentStateChangedEvent
= new object ();
177 /// Raised when transition begins
179 public event EventHandler
<VisualStateChangedEventArgs
> CurrentStateChanging
{
181 RegisterEvent (CurrentStateChangingEvent
, "CurrentStateChanging", Events
.current_state_changing
, value);
184 UnregisterEvent (CurrentStateChangingEvent
, "CurrentStateChanging", Events
.current_state_changing
, value);
189 /// Raised when transition ends and new state storyboard begins.
191 public event EventHandler
<VisualStateChangedEventArgs
> CurrentStateChanged
{
193 RegisterEvent (CurrentStateChangedEvent
, "CurrentStateChanged", Events
.current_state_changed
, value);
196 UnregisterEvent (CurrentStateChangedEvent
, "CurrentStateChanged", Events
.current_state_changed
, value);
201 private Collection
<Storyboard
> _currentStoryboards
;
202 private Collection
<VisualState
> _states
;
203 private Collection
<VisualTransition
> _transitions
;
204 private string _name
= String
.Empty
;