title: WPF: VisualStateManager not working
body: <p>I'm trying to figure out how the VisualStateManager works but I cannot do it.</p>
  
  <p>I want to do this: I have some TextBoxes where the user can enter his name, adress, ... and I'm doing a validation and I didn't want to do that with a validationrules-binding in XAML. I'm using MVVM-pattern and some code of my viewmodel does it. So I'm binding the text of the textbox to a string that the viewmodel validated for me and I have a boolean variable there that tell if it's correct or not. </p>
  
  <p>My XAML:</p>
  
  <pre><code>&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:Custom="http://schemas.microsoft.com/surface/2008"
  mc:Ignorable="d"
  x:Class="Surface_Bachelorarbeit.NeuerUser"
  x:Name="UserControl"
  UseLayoutRounding="True"
  d:DesignWidth="640" d:DesignHeight="480" Width="800" Height="350"&gt;
  &lt;Border x:Name="Border1" BorderBrush="Black" BorderThickness="5" CornerRadius="5" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}"&gt;
      &lt;VisualStateManager.VisualStateGroups&gt;
          &lt;VisualStateGroup x:Name="VisibilityStates"&gt;
              &lt;VisualStateGroup.Transitions&gt;
                  &lt;VisualTransition GeneratedDuration="0" To="NameValid"&gt;
                      &lt;Storyboard&gt;
                          &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid"&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/&gt;
                          &lt;/ObjectAnimationUsingKeyFrames&gt;
                          &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid"&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/&gt;
                          &lt;/ObjectAnimationUsingKeyFrames&gt;
                      &lt;/Storyboard&gt;
                  &lt;/VisualTransition&gt;
                  &lt;VisualTransition GeneratedDuration="0" To="NameInvalid"&gt;
                      &lt;Storyboard&gt;
                          &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid"&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/&gt;
                          &lt;/ObjectAnimationUsingKeyFrames&gt;
                          &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid"&gt;
                              &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/&gt;
                          &lt;/ObjectAnimationUsingKeyFrames&gt;
                      &lt;/Storyboard&gt;
                  &lt;/VisualTransition&gt;
              &lt;/VisualStateGroup.Transitions&gt;
              &lt;VisualState x:Name="NameValid"/&gt;
              &lt;VisualState x:Name="NameInvalid"/&gt;
  
  
          &lt;/VisualStateGroup&gt;
      &lt;/VisualStateManager.VisualStateGroups&gt;
  ...
          &lt;Custom:SurfaceTextBox x:Name="vname" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Margin="0,1,22,8" FontSize="14.667" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" DataContext="{Binding Forename}" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" LostFocus="vname_LostFocus"/&gt;
  ...
                  &lt;Image x:Name="VN_valid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Source="valid.png" Height="22" VerticalAlignment="Top" d:LayoutOverrides="Height"/&gt;
              &lt;Image x:Name="VN_invalid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Height="22" Source="invalid.png" VerticalAlignment="Top" d:LayoutOverrides="Height"/&gt;
   ...
  </code></pre>
  
  <p>When the user's focus leaves the textbox, this event shall happen:</p>
  
  <pre><code>    private void vname_LostFocus(object sender, System.Windows.RoutedEventArgs e)
      {
          mvm = ((MainViewModel)FindResource("MainViewModelDataSource"));
          if (mvm.NewCreatedStoreUser.Forename.Valid)
          {
              VisualStateManager.GoToState(this, "NameValid", true);
  
          }
          else 
          {
              VisualStateManager.GoToState(this, "NameInvalid", true);
  
          }
  
       }
  </code></pre>
  
  <p>I tried it and the state is not changed, although the event happens and it goes to the right GoToState-Method. Nothing happens. </p>
  
  <p>I also tried<br>
       ExtendedVisualStateManager.GoToElementState(this.Border1 as FrameworkElement, "NameValid", false);</p>
  
  <p>because I read here that it would help but it's just the same with that...</p>
  
  <p>What did I do wrong??</p>