2

我有一个 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吗?

4

1 回答 1

2

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

于 2011-05-05T00:44:27.967 回答