0

I have my XAML code as below.

                <ControlTemplate TargetType="{x:Type local:ThumbnailItem}">
                    <ContentPresenter>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="FocusedStates">
                                <VisualState Name="Selected">
                                    <Storyboard>
                                        <ColorAnimation To="Red" 
              Storyboard.TargetProperty="Background"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </ContentPresenter>
                </ControlTemplate>

I want to access the defined VisualStateGroups from code behind. But the below code VisualStateManager.GetVisualStateGroups is always returns null.

 ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(this);
 var visualStateGroups = VisualStateManager.GetVisualStateGroups(myContentPresenter);

Is there any good way to achieve it?

DeepakG
  • 17
  • 6
  • Does this answer your question? [How do I access an element of a control template from within code-behind](https://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind) – Rekshino Mar 02 '20 at 10:24
  • No. I can access the elements, but I need to read the visual state groups and its values present in the same. – DeepakG Mar 02 '20 at 10:29
  • `MyControl.Template.FindName("FocusedStates", MyControl) as VisualStateGroup` does access the `VisualStateGroup`. – Rekshino Mar 02 '20 at 10:36
  • Thank you. This gives the information. I get the VisualState too using the name "Selected". Meanwhile, Can I get the Color animation inside the VisualState using the object? – DeepakG Mar 02 '20 at 11:05

0 Answers0