Sunday, March 6, 2011

VisualStateManager.GoToState not working?

I’ve had issues with this on and off for a while and thought I should note my solution quickly.

 

Expression Blend will actually use ExtendedVisualStateManager, but the base class VisualStateManager has this solution as well. Basically, due to a bug that was supposed to be fixed, but for whatever reason still exists, the visual state sometimes won’t change using this method. The GoToState method takes a control, a state name string, and Boolean whether to use transition animations.

 

The solution is simple. There is another method called GotoElementState that takes a FrameworkElement, and the rest. Just use this instead and your problems go away.

 

   1: // Avoid this.
   2: VisualStateManager.GoToState(ctrl, stateName, true);
   3:  
   4: // Use this.
   5: VisualStateManager.GoToElementState(ctrl, stateName, true);

2 comments: