找不到VisualStateGroups?

时间:2011-05-04 23:48:23

标签: c# silverlight visualstategroup

我有一个ui控件的XAML(从一个按钮继承)并且在其中有各种可视状态组,如下所示:

                            <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>

在onapplytemplte函数的代码隐藏中,我尝试:

VisualStateManager.GetVisualStateGroups(this).Count

但返回值为'0',不应该是2?

1 个答案:

答案 0 :(得分:2)

VisualStateManager.VisualStateGroups设置在控件的控件模板中的根元素上(即this),而不是控件本身。您需要为您的根元素设置x:Name,然后使用GetTemplateChild访问它。