I need to change state of a control and then do some action. To be specific, I want to run an animation before a control is hidden. I would like to do something like that:
It is in fact possible to attach the Completed handler in code:
Collection grps = (Collection)VisualStateManager.GetVisualStateGroups(this.LayoutRoot);
foreach (VisualStateGroup grp in grps) {
Collection states = (Collection)grp.States;
foreach (VisualState state in states) {
switch (state.Name) {
case "Intro":
state.Storyboard.Completed += new EventHandler(Intro_Completed);break;
}
}
}
Example from this thread: http://forums.silverlight.net/forums/p/38027/276746.aspx
Working for me in live project using attached Behavior too! Slightly annoying though that I had to use separate dependency properties for the root UserControl (to use in VisualStateManager.GoToState) and the LayoutRoot to get the actual VisualStateGroup collection.