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.
#1 by David on October 22, 2009 - 11:58 pm
Quote
Thank goodness someone found a workaround for this!
#2 by aimee on October 25, 2009 - 7:44 pm
Quote
thank you so much for posting this
#3 by Tyler on October 30, 2009 - 7:39 am
Quote
Solved an issue that I’d been struggling with for hours. Excellent post. Thanks!
Pingback: Managing WPF Screens with Windows 7 Jumplists and the Visual State Manager | Aimee Gurl...
#4 by Lee on November 25, 2009 - 6:57 am
Quote
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.
#5 by Carl Scarlett on January 4, 2010 - 12:40 pm
Quote
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!
#6 by Arona on May 8, 2010 - 5:59 am
Quote
Thanks ! you know……..; youSolved an issue that I’d been struggling with for hours……..
Great POst
#7 by Rudi on May 17, 2010 - 3:59 am
Quote
Thank you for your great help!
This ‘Workaround’ is still needed in the VS 2010 RC!
What is MS doing??
Great Post
#8 by Denis on May 27, 2010 - 12:59 am
Quote
Thanks!!!
#9 by Edu001 on June 16, 2010 - 8:39 am
Quote
I was going crazy on this.. thanks a lot.
#10 by Kasimier on June 16, 2010 - 11:35 am
Quote
Actually the following works for me currently:
VisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, “MyState”, true);
Note the GoToElementState(..) instead of GoToState(..)
Thanks again!
#11 by Sloven on August 14, 2010 - 12:08 am
Quote
It doesn’t work for me. Nothing happens inside the control.
VisualStateManager.GoToElementState returns false
#12 by Sloven on August 14, 2010 - 12:28 am
Quote
Silly me. I should use sender as FrameworkElement. All Fine!
#13 by stanque on January 6, 2011 - 10:02 am
Quote
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.
#14 by Joel Cochran on March 29, 2011 - 5:58 am
Quote
Kudos and thanks!
#15 by Xin on April 18, 2011 - 9:40 am
Quote
it doesn’t work because GoToState accepts a Control, and a Grid (your LayoutRoot) in your case, is not a Control.
#16 by mojo on May 6, 2011 - 12:38 am
Quote
Fantastic, thanks!!
#17 by Franck on June 17, 2011 - 4:30 am
Quote
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.
#18 by seyd ali khameneyee on September 27, 2011 - 11:00 pm
Quote
Oh my God !!!
thanks a lot , it solved my problem too !
اجرکم عندالله محفوظ