title: Visual state not changing when control invisible
body: <p>I've a button control that's got 2 visual states; say "Simple" and "Complex"; the former being the default. Initially the control is hidden.</p>
  
  <p>When I set the visual state through <em>VisualStateManager</em> to "Complex", I get <code>false</code>, but when I make the control visible once and then set the visual state, it returns <code>true</code> and the state is preserved (even after the control becoming invisible) i.e. all that matters is if the control was shown atleast once; if so, the visual state changes are respected, else it's always ignored until it's shown once.</p>
  
  <p>How do I set the visual state for a control that's yet to be visible?</p>
  
  <pre><code>&lt;Grid x:Name="LayoutRoot"
        Background="LightBlue"&gt;
    &lt;Grid.Resources&gt;
      &lt;Style x:Key="MyButton"
             TargetType="Button"&gt;
        &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType="Button"&gt;
              &lt;Grid&gt;
                &lt;VisualStateManager.VisualStateGroups&gt;
                  &lt;VisualStateGroup x:Name="MyStates"&gt;
                    &lt;VisualState x:Name="Simple" /&gt;
                    &lt;VisualState x:Name="Complex"&gt;
                      &lt;Storyboard&gt;
                        &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
                                                       Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"&gt;
                          &lt;DiscreteObjectKeyFrame KeyTime="10"
                                                  Value="#00000000" /&gt;
                        &lt;/ObjectAnimationUsingKeyFrames&gt;
                      &lt;/Storyboard&gt;
                    &lt;/VisualState&gt;
                  &lt;/VisualStateGroup&gt;
                &lt;/VisualStateManager.VisualStateGroups&gt;
                &lt;Ellipse x:Name="ellipse"
                         Fill="#FF0E0EF5"
                         Stroke="Black" /&gt;
                &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /&gt;
              &lt;/Grid&gt;
            &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
      &lt;/Style&gt;
    &lt;/Grid.Resources&gt;
    &lt;Button x:Name="btnStuff"
            Content="Click Me!"
            Visibility="Collapsed"
            Style="{StaticResource MyButton}" /&gt;
    &lt;StackPanel&gt;
      &lt;Button x:Name="btnShow"
              Content="Show"
              Click="btnShow_Click" /&gt;
      &lt;Button x:Name="btnVisual"
              Content="Visual Change"
              Click="btnVisual_Click" /&gt;
    &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>Code behind:</strong></p>
  
  <pre><code>    bool fOriginalState = true;
  
      private void btnShow_Click(object sender, RoutedEventArgs e)
      {
          btnStuff.Visibility = btnStuff.Visibility == Windows.UI.Xaml.Visibility.Visible ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
      }
  
      private void btnVisual_Click(object sender, RoutedEventArgs e)
      {
          string visualState = fOriginalState ? "Complex" : "Simple";
          fOriginalState = !fOriginalState;
          bool fSet = VisualStateManager.GoToState(btnStuff, visualState, false);
          System.Diagnostics.Debug.WriteLine(fSet.ToString());
      }
  </code></pre>