Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit VisualStateManager.GoToState not changing states sometimes
9 replies. Latest Post by aquamoon on September 3, 2008.
(0)
pbrooks
Contributor
2671 points
355 Posts
06-15-2008 6:50 PM |
I am working on a custom control that uses VisualStateManager.GoToState to change to its various states (MouseOver, Normal). It works fine for a few state changes but then it inevitably seems to silently fail for some state changes. The strange thing is that the behavior is only occuring when I have multiple instances of the same control on my form. I haven't been able to pin down why this is happening, but VisualStateManager.GoToState is returning true even when it fails to change state. Also, the control goes back to the correct state when I resize the browser window. Anybody have any ideas about this? Thanks!
Dave Relyea
Participant
1084 points
249 Posts
06-16-2008 11:00 PM |
If you are animating the color of a SolidColorBrush, we have some issues with that, since a feature to share brushes isn't playing nicely with VSM.
06-17-2008 12:02 AM |
Wow, I spent at least 4 hours trying to figure out what I was doing wrong and I think you nailed it right on the head (at least I hope). I changed my states to temporarily use Opacity and the states do seem to function correctly. In other words, I can move my mouse as fast as I want and for as long as I want and the controls seem to transition to the correct states. I am using ColorAnimation for my states and I am animating
(Shape.Fill).(GradientBrush.GradientStops)[X].(GradientStop.Color)(Shape.Stroke).(GradientBrush.GradientStops)[X].(GradientStop.Color)
Does the issue you describe apply to this as well? If so, is there something I can do to work around it? Thanks a bunch for your help on this, I have been pulling my hair out!
Yi-Lun L...
All-Star
25052 points
2,747 Posts
06-17-2008 3:30 AM |
Hello, I think to work around the issue Dave described, you need to create multiple elements for different groups. For example, with a CheckBox, if you use a single Rectangle for both Mouse Over and Checked state, and animate its Fill, you'll run into problems because the two states are in different groups. Instead, you can create two Rectangles with proper ZIndex. You animate the Opacity or Visibility property of those Rectangles. For your Custom Control, you should be careful with groups, so states in one group won't interfere with each other.
06-17-2008 8:33 AM |
My custom control only has one VSM group, so I was thinking that there was no way my states could interfere with each other. My control is a star rating control, similar to what you would see at Netflix. If you hover your mouse over the stars, all the preceding stars (including the mouse over star) move to the highlighted state. The states are ColorAnimations that change from a dull grey to a bright yellow. The issue I am having is when I move my mouse over the last star it hangs on the state. The funny thing is that it doesn't do it right away, it only does it after moving the mouse around for a while. That is why I thought it might be a resource sharing problem. Since changing the opacity seems to work without hanging up on the states, I guess I could do what you said and create 2 stars, normal and highlighted, and toy with the opacity. I just feel like it would be more elegant if I could use Color animations instead. I would like to know more about the SolidColorBrush/VSM issue that Dave mentioned so I can be sure to avoid it in the future. Thanks again for any help!
06-17-2008 9:47 PM |
I built a sample control that exhibits the same behavior as my other control so you can get an idea for what I am trying to explain. If you move your mouse back and forth across the lights, you will see they turn on in an incremental fashion. If you move back and forth a good bit, eventually the last light will hang in the "on" state. If you play around with it enough, other random lights will hang in the "on" state. See the screenshots below:
While re-building this sample, I learned that the problem only occurs (as far as I can tell) if my Visual State contains a ColorAnimation that targets a GradientBrush. I used the exact same code with a simple color animation (SolidColorBrush) and it works as expected. I've included the source code and a live demo. Thanks for any help you can offer on this!
Live Demo:http://pagebrooks.com/demos/ColorAnimationVSM/SilvelightLightBulbMatrixTestPage.html
Source Code:http://pagebrooks.com/files/source/silverlight/coloranimationvsm.zip
06-18-2008 11:50 PM |
I think it is the same issue. This works fine for me (for as long as I tried it). You could also use a Grid as your root and put another ellipse with the "off" color in front of the "on" ellipse, and animate the opacity or visibility of that.
</Setter>
06-19-2008 8:09 AM |
Is this something that will be fixed before release? I was able to accomplish my goal with the said opacity workaround. Unfortunately, that's just not as elegant as I would like because in my real application, I am animating the colors of a complicated Path and I just don't really like having all that duplicated XAML (on/off states) in there. Either way, I'm going to mark your reply as 'answered' since it does fix the problem, but it would be nice to know if this is in fact a bug or simply a limitation of Silverlight. Thanks a bunch!
06-19-2008 9:58 AM |
Oh, we're working on it.
aquamoon
Member
2 points
2 Posts
09-03-2008 5:37 AM |
another workaround move the storyboard to your usercontrol.resources, give it a name and start it in code with (Resources["MyName"] as Storyboard).Begin();
(Resources[