Blend4 - Should VSM "GoToState" Work in the Loaded Event Handler (Code Behind)? RRS feed

  • Question

  • To familiarize myself with VSM, I created a simple example to test the implementation of state transitions.  My example is a rectangle inside of a grid, inside of my MainWindow.  When I mouseover the rectangle I want to change color.  I defined the respective VSM states from Blend4, and then opened my code-behind file to add the event handlers in the MainWindow constructor.  Unfortunately I can't seem to get my mouseover transition to fire off.  It seems as if WPF is ignoring my "GoToState" calls.  Am I missing something?  (I realize that I can implementat all of this using the "GoToStateAction" behavior directly in XAML, but due to some legacy code issues I need to work with event handlers in the code behind.)  Here's my *.cs code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    namespace GoToState__in_Code_Behind
    	public partial class MainWindow : Window
    		public MainWindow()
          Loaded += new RoutedEventHandler(MainWindow_Loaded);
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
          VisualStateManager.GoToState(this, "MyMouseLeave", true);
          LayoutRoot.MouseEnter += new MouseEventHandler(LayoutRoot_MouseEnter);
          LayoutRoot.MouseLeave += new MouseEventHandler(LayoutRoot_MouseLeave);
        void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
          VisualStateManager.GoToState(this, "MyMouseLeave", true);
        void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
          VisualStateManager.GoToState(this, "MyMouseEnter", true);

    Here's my XAML:

    	Width="640" Height="480">
    	<Grid x:Name="LayoutRoot" Width="125" Height="75">
    			<VisualStateGroup x:Name="MyVisualStateGroup">
    				<VisualState x:Name="MyDefault"/>
    				<VisualState x:Name="MyMouseEnter">
    						<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
    							<SplineColorKeyFrame KeyTime="0" Value="#FF1D659B"/>
    				<VisualState x:Name="MyMouseLeave"/>
    		<Rectangle x:Name="rectangle" Fill="#FFAED8F7" Stroke="{x:Null}"/>
    Monday, May 24, 2010 6:12 PM

All replies

  • Can you see if this works for you:

    1. Change states using a call of this form:



    "MyMouseLeave", true);

    (note the use of GoToElementState and not GoToState, and the passing of LayoutRoot as the first argument instead of this).

    2. You don't need the CustomVisualStateManager any longer.

    If you were to use GTSAction (I realize you can't but if you could) then it would call GTElementState (AFAIK).

    Monday, May 24, 2010 7:43 PM
  • Thanks, Steve.  When I implemented #1 above, I get the error, 'System.Windows.VisualStateManager' does not contain a definition for 'GoToElementState'.  Thoughts?

    Tuesday, May 25, 2010 3:33 PM
  • Is this only available in .NET 4?  I'm running 3.5.
    Tuesday, May 25, 2010 3:38 PM
  • Microsoft.Expression.Interactivity.Core.ExtendedVisualStateManager
    Monday, May 30, 2011 9:38 PM