Workaround for VisualStateManager.GoToState not working on Window

I am not sure if this is a bug or what, but I’ve had an annoying problem today whereby it seems you are unable to switch states procedurally using code like this:

VisualStateManager.GoToState(this, "MyState", true);

when your control (the first parameter) is not a UserControl. In my case that control was the top level Window of my WPF application.

The strange thing is the state switching works fine when I use the GoToStateAction.

After pouring over the code in Reflector it strikes me that something I don’t yet understand is happening with CustomVisualStateManager which Blend actually sets to be ExtendedVisualStateManager.

It seems ExtendedVisualStateManager has a method called GoToElementState which allows you to pass in a FrameworkElement, whereas GotToState only allows a Control.

By trial and error I found that the following code works:

ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "MyState", true);

where Layroot root is a Grid and is the first child of my Window, and is where the actual VisualStateGroups are defined.

If anyone has an idea what is going on here, and why the GoToState doesn’t work as expected for a Window, please let me know.

Update: According to a comment from Steve at Microsoft, this issue will be fixed when the Visual State Manager gets integrated into .NET Base Class Libraries (BCL) e.g. when WPF 4.0 is released.

27 thoughts on “Workaround for VisualStateManager.GoToState not working on Window

  1. Pingback: Managing WPF Screens with Windows 7 Jumplists and the Visual State Manager | Aimee Gurl...

  2. Lee

    Good job finding this. It was a most irksome bug! Made me write code I wasn’t proud of… that other developers had to see. :(

  3. Carl Scarlett

    This also resolves an issue with an exception “Target does not define any VisualStateGroups” that I’ve had copying XAML from Blend 3 to an extended window control.

    Rather than using (which causes the exception at ) I’ve added an event handler and used the ExtendedVisualStateManager.GoToElementState workaround.

    Thanks for this!

  4. Arona

    Thanks ! you know……..; youSolved an issue that I’d been struggling with for hours……..

    Great POst

  5. Rudi

    Thank you for your great help!

    This ‘Workaround’ is still needed in the VS 2010 RC!
    What is MS doing??

    Great Post

  6. Kasimier

    Actually the following works for me currently:

    VisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, “MyState”, true);

    Note the GoToElementState(..) instead of GoToState(..)

    Thanks again!

  7. Sloven

    It doesn’t work for me. Nothing happens inside the control.
    VisualStateManager.GoToElementState returns false

  8. stanque

    Thanks!

    This solved problem I had in my Silverlight 4 project. I have found so far that using WPF and Silverlight 4.0 VisualStateManager.GotoState() method really behaves quite unpredictable so most of the time I avoided using it. Thanks for great workaround.

  9. Xin

    it doesn’t work because GoToState accepts a Control, and a Grid (your LayoutRoot) in your case, is not a Control.

  10. Franck

    I still have this issue in WPF4. Although now GoToElementState exists on VisualStateManager, so you can us it instead of using the Blend ExtendedVisualStateManager if you are working directly in VisualStudio.

  11. Jeff

    Thanks for sharing! Worked like a charm! I believe the same bug exists in Silverlight and I could not figure out why it was not working.

  12. Max Wurm

    THAAANK YOU!!! It works!!!
    You saved me from not sleeping at all.

    Don’t know how you found this solution and I do not understand it either, but good job.

  13. serkan

    VisualStateManager.GoToState(MotorButton, “MotorTestPointerOut”, true);

    insted of this use element name it must work ;)

  14. Sev

    Thanks a Lot for this.
    I tried to find the mistake for hours.
    Never thought i gonna solve it :D
    thanks a lot!

  15. Adam

    WTF WPF

    But thanks for the fix and for doing this research. I was also about to head down the path of ugly code. If this wasn’t going to work I wish it would throw an exception instead of just doing nothing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>