title: How to programatically change the visual state in silverlight
body: <p>I have the following code (it's just the Blend ControlTemplate of a ListBoxItem plus two new states and an event handler to ease the example):</p>
  
  <p>xaml:</p>
  
  <pre><code>        &lt;StackPanel&gt;
          &lt;Button Click="ButtonBase_OnClick" Content="Clicle"/&gt;
          &lt;ListBox Grid.Row="1" Margin="12,0,12,0" x:Name="list"&gt;
              &lt;ListBox.Resources&gt;
                  &lt;Style TargetType="ListBoxItem"&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="ListBoxItem"&gt;
                                  &lt;Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" MouseLeftButtonDown="LayoutRoot_OnMouseLeftButtonDown"&gt;
                                      &lt;VisualStateManager.VisualStateGroups&gt;
                                          &lt;VisualStateGroup x:Name="CommonStates"&gt;
                                              &lt;VisualState x:Name="Normal"/&gt;
                                              &lt;VisualState x:Name="MouseOver"/&gt;
                                              &lt;VisualState x:Name="Disabled"/&gt;
                                          &lt;/VisualStateGroup&gt;
                                          &lt;VisualStateGroup x:Name="SelectionStates"&gt;
                                              &lt;VisualState x:Name="Unselected"/&gt;
                                              &lt;VisualState x:Name="Selected"/&gt;
                                          &lt;/VisualStateGroup&gt;
                                          &lt;VisualStateGroup x:Name="Custom"&gt;
                                              &lt;VisualState x:Name="NotSet"/&gt;
                                              &lt;VisualState x:Name="Set"/&gt;
                                          &lt;/VisualStateGroup&gt;
                                      &lt;/VisualStateManager.VisualStateGroups&gt;
                                      &lt;ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/&gt;
                                  &lt;/Border&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/ListBox.Resources&gt;
          &lt;/ListBox&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>Then I the code behind I'm trying to set one of these new states:</p>
  
  <pre><code>    private void LayoutRoot_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          bool result = ExtendedVisualStateManager.GoToElementState(sender as FrameworkElement, "Set", true);
      }
  </code></pre>
  
  <p>Whateever I try I always get <code>result = false</code> so the state is not changed. As I'm pretty new to Visual States what I'm doing wrong?</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>I also tried the following code to use the ListBoxItem but it also fails:</p>
  
  <pre><code>        DependencyObject parent = VisualTreeHelper.GetParent(sender as FrameworkElement);
          var a = ExtendedVisualStateManager.GoToElementState(parent as FrameworkElement, "Set", false);
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I think I found the problema while looking at the VisualStateManager. It checks that the VisualStates exist and currently the states are declared into the Border, not into the ListBoxItem (checked that calling VisualStateManager.GetVisualGroups). Any idea about why?</p>
  
  <p>Thanks in advance.</p>