Search code examples
c#silverlightvisualstategroup

VisualStateGroups not found?


I have a XAML of a ui control (which inherits from a button) and have various visual state groups in it as follows:

                            <vsm:VisualStateGroup x:Name="CommonStates">
                                <vsm:VisualState x:Name="Normal"/>
                                <vsm:VisualState x:Name="MouseOver">
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="Green" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.Background).(SolidColorBrush.Color)" />
                                        <DoubleAnimation Duration="0" To="0.94" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" />
                                        <DoubleAnimation Duration="0" To="0.94" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" />
                                    </Storyboard>
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Disabled">
                                    <Storyboard/>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>

                            <vsm:VisualStateGroup x:Name="MyState">
                                <vsm:VisualState x:Name="Pressed_Green">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="Yellow" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.Background).(SolidColorBrush.Color)" />
                                        <DoubleAnimation Duration="0" To="0.94" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" />
                                        <DoubleAnimation Duration="0" To="0.94" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" />
                                    </Storyboard>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>




                        </vsm:VisualStateManager.VisualStateGroups>

In codebehind in the onapplytemplte function I try:

VisualStateManager.GetVisualStateGroups(this).Count

But the return value is '0', shouldn't it be 2?


Solution

  • The VisualStateManager.VisualStateGroups is set on the root element in the control template of your control (i.e. this), not on the control itself. You would need to give your root element an x:Name and then access it using GetTemplateChild.