locked
GoToState doesn't work with WPF ( ...but does in Silverlight ) ? RRS feed

  • Question

  • Hi,

    Having difficulty switching between States in a WPF using C# (... the code works fine when I use it in Silverlight);  I'm triggering the gotostate through a button 'Click' Event...

    private void clicked1 (object sender, System.Windows.RoutedEventArgs e)
            {VisualStateManager.GoToState (this,"VisualState1",true);}

    ... It builds fine, no error message, loads up - but - clicking the button does nothing (no state change, although I know the event does fire). [Note: I have the latest WPFToolkit.dll referenced in project and don't wish to use GoToStateAction behaviour because I also want to trigger states via if/else statements eventually.]

    Many Thanks for your time,
    Rhodz,
    • Edited by dt01rrj Friday, February 26, 2010 10:39 PM added info
    Friday, February 26, 2010 10:31 PM

Answers

  • Are you doing this on a Window or UserControl?

    VSM is really meant to work as states on a usercontrol, what you are attempting to do should work fine if the item is a usercontrol (meaning the this parameter should refer to a usercontrol).

    If you want to do this on a window, the following code should work:

    Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.GoToElementState(this.LayoutRoot, "Blue", true);

    If you examine the xaml of your window, the vsm states get defined under layoutroot, which is a grid by default. The VSM.GoToState() call requires a Control derived class, which Grid is not.

    Hope that helps.
    • Proposed as answer by Chuck HaysModerator Monday, March 1, 2010 2:31 PM
    • Marked as answer by dt01rrj Monday, March 1, 2010 8:31 PM
    Monday, March 1, 2010 2:31 PM
    Moderator

All replies

  • Are you doing this on a Window or UserControl?

    VSM is really meant to work as states on a usercontrol, what you are attempting to do should work fine if the item is a usercontrol (meaning the this parameter should refer to a usercontrol).

    If you want to do this on a window, the following code should work:

    Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.GoToElementState(this.LayoutRoot, "Blue", true);

    If you examine the xaml of your window, the vsm states get defined under layoutroot, which is a grid by default. The VSM.GoToState() call requires a Control derived class, which Grid is not.

    Hope that helps.
    • Proposed as answer by Chuck HaysModerator Monday, March 1, 2010 2:31 PM
    • Marked as answer by dt01rrj Monday, March 1, 2010 8:31 PM
    Monday, March 1, 2010 2:31 PM
    Moderator
  • Greetings Chuck,

    Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager.GoToElementState(this
    .LayoutRoot, "Blue"
    , true
    );
    ... It works!!! :)  ... I've spent countless frustrated hours trying to figure this out - my entire UI relied on this missing bit of code, this is a MASSIVE help, thankyou! You deserve a medal!

    Many, Many, Many Thanks!

    Best Regards,

    -RJ-
    Monday, March 1, 2010 8:31 PM
  • ... Just as an addition, the above piece of code only works if the first element (... be it a grid, viewbox, canvas etc) is named/labelled as "LayoutRoot", anything else and the code will do nothing.

     

    Cheers,

    -RJ-

    Thursday, June 3, 2010 11:42 AM
  • Hi,

    Same problem I am Facing in WPF for Custom Controls. VSM GoToState didn't work so please help me in that.........

     

    Tuesday, August 2, 2011 10:26 AM