title: Skip state depending on property in View Model
body: <p>I'm pretty new to MVVM so i assume this is basics. It's a Windows 8.1 app with MVVM light and a Sqlite DB.</p>
  
  <p>I've got a page that contain 4 States. Each State contain a GridView where you can select an item to set a bound property. On the SelectionChanged I go to the next State.</p>
  
  <p>Here's the GridView's xaml :</p>
  
  <pre><code> &lt;GridView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Essences}" SelectedItem="{Binding SelectedEssense,Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="EssenceGridView" Opacity="0" &gt;
          &lt;Interactivity:Interaction.Behaviors&gt;
              &lt;Core:EventTriggerBehavior EventName="SelectionChanged"&gt;
                  &lt;Core:GoToStateAction StateName="Diametre"/&gt;
              &lt;/Core:EventTriggerBehavior&gt;
          &lt;/Interactivity:Interaction.Behaviors&gt;
          &lt;GridView.ItemTemplate&gt;
              &lt;DataTemplate &gt;
                  &lt;Grid Width="250" Height="80"&gt;
                      &lt;TextBlock Text="{Binding Trigramme}" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;&lt;/TextBlock&gt;
                  &lt;/Grid&gt;
              &lt;/DataTemplate&gt;
          &lt;/GridView.ItemTemplate&gt;
      &lt;/GridView&gt;
  </code></pre>
  
  <p>The StoryBoards that change States are just Opacity changing.</p>
  
  <p>Now I want to be able to totaly skip one State, because the user want to be able to use some defaults properties (so he don't have to select the same one all the time)
  So, if the default property is set in my ViewModel, I want to able to skip one state. 
  What is the best approach to achieve this in MVVM ?</p>
  
  <p>Edit : With the help of the link of Depechie, I successfully bind my VisualState to property in my ViewModel. I had to update the code because it was for Windows phone 8 and i'm working with Windows 8.1 !!</p>
  
  <p>Here the updated class :
  BindVisualStateBehaviorHandler.cs</p>
  
  <pre><code>class BindVisualStateBehaviorHandler : Behavior&lt;FrameworkElement&gt;
  {
      //
      // Dependency property "StateName" that you can bind in Blend.
      // Bind this to the enumeration that controls the visual state.
      //
      public static DependencyProperty StateNameProperty = DependencyProperty.Register(
          "StateName",
          typeof(string),
          typeof(BindVisualStateBehaviorHandler),
          new PropertyMetadata(null, StateNamePropertyChanged));
  
      public string StateName
      {
          get { return (string)GetValue(StateNameProperty); }
          set { SetValue(StateNameProperty, value); }
      }
  
      //
      // When the StateName property changes, switch to the
      // new visual state, and play transition animations.
      //
      private static void StateNamePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
      {
          ((BindVisualStateBehaviorHandler)obj).UpdateVisualState(
              (string)args.NewValue,
              useTransitions: true);
      }
  
      //
      // When the behavior is first attached, go to the visual state,
      // but don't play any animations.
      //
      protected override void OnAttached()
      {
          UpdateVisualState(
              StateName,
              useTransitions: false);
          base.OnAttached();
      }
  
      private void UpdateVisualState(string visualState, bool useTransitions)
      {
          if (AssociatedObject != null)
          {
              if (VisualStateUtilities.FindNearestStatefulControl(base.AssociatedObject as FrameworkElement) != null)
              {
                  VisualStateUtilities.GoToState(
                      VisualStateUtilities.FindNearestStatefulControl(base.AssociatedObject as FrameworkElement),
                      visualState,
                      useTransitions);
              }
          }
      }
  }
  </code></pre>
  
  <p>I had to use my own Behavior cuz it was not présent in Microsoft.Xaml.Interactivity</p>
  
  <p>Behavior.cs</p>
  
  <pre><code> public abstract class Behavior&lt;T&gt; : DependencyObject, IBehavior where T : DependencyObject
  {
      //http://reflectionit.nl/Blog/2013/windows-8-xaml-tips-creating-blend-behaviors
  
      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
      public T AssociatedObject { get; set; }
  
      protected virtual void OnAttached()
      {
      }
  
      protected virtual void OnDetaching()
      {
      }
  
      public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
      {
          this.AssociatedObject = (T)associatedObject;
          OnAttached();
      }
  
      public void Detach()
      {
          OnDetaching();
      }
  
      DependencyObject IBehavior.AssociatedObject
      {
          get { return this.AssociatedObject; }
      }
  }
  </code></pre>
  
  <p>Then in my View :</p>
  
  <pre><code> &lt;VisualStateManager.VisualStateGroups&gt;
          &lt;VisualStateGroup x:Name="AppStates" &gt;
              &lt;VisualStateGroup.Transitions&gt;
                  &lt;VisualTransition GeneratedDuration="0:0:0.2"/&gt;
              &lt;/VisualStateGroup.Transitions&gt;
              &lt;VisualState x:Name="SaisieTige" &gt;
                  ...
              &lt;VisualState x:Name="Diametre"&gt;
                  ...
              &lt;/VisualState&gt;
              &lt;VisualState x:Name="Hauteur"&gt;
                 ...
              &lt;/VisualState&gt;
              &lt;VisualState x:Name="Resume"&gt;
                  ...
              &lt;/VisualState&gt;
          &lt;/VisualStateGroup&gt;
      &lt;/VisualStateManager.VisualStateGroups&gt;
      &lt;Interactivity:Interaction.Behaviors&gt;
          &lt;Behavior:BindVisualStateBehaviorHandler x:Name="AppStateBehavior" StateName="{Binding StateHandler.AppState,Mode=TwoWay}"&gt;&lt;/Behavior:BindVisualStateBehaviorHandler&gt;
          &lt;Core:EventTriggerBehavior EventName="Loaded"&gt;
              &lt;Core:InvokeCommandAction Command="{Binding SelectEssenceOrNotCommand}"&gt;&lt;/Core:InvokeCommandAction&gt;
          &lt;/Core:EventTriggerBehavior&gt;
      &lt;/Interactivity:Interaction.Behaviors&gt;
  </code></pre>
  
  <p>I have to call the RaisePropertyCHange each time i change the State :</p>
  
  <pre><code> StateHandler.SetAppState(AppStates.SaisieTige);
   RaisePropertyChanged("StateHandler");
  </code></pre>
  
  <p>ANd Now it's working, thx a lot guys !!!</p>