如何在可见度变化时将silverlight控件的视觉状态重置为“正常”

时间:2011-05-06 14:47:48

标签: silverlight

我的按钮控件有滚动效果问题。我在点击时折叠按钮,稍后它再次变为可见,但当按钮变为可见时,鼠标悬停状态不会变回“正常”。但是,可以通过更改代码中的可视状态来解决该问题,但我想知道是否有办法在控件模板中执行此操作? 这是我的模板:

<Style x:Key="replayButton" TargetType="Button">
        <Setter Property="Background" Value="#FF1F3B53"/>
        <Setter Property="Foreground" Value="#FF000000"/>
        <Setter Property="Padding" Value="3"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFA3AEB9" Offset="0"/>
                    <GradientStop Color="#FF8399A9" Offset="0.375"/>
                    <GradientStop Color="#FF718597" Offset="0.375"/>
                    <GradientStop Color="#FF617584" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Cursor="Hand" Height="48" Margin="0,0,0,0" Width="48">
                        <vsm:VisualStateManager.VisualStateGroups>
                            <vsm:VisualStateGroup x:Name="CommonStates">
                                <vsm:VisualState x:Name="Normal">
                                    <Storyboard Storyboard.TargetName="bgImg" Storyboard.TargetProperty="Source">
                                        <ObjectAnimationUsingKeyFrames>
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="images/repeat.png"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="MouseOver">
                                    <Storyboard Storyboard.TargetName="bgImg" Storyboard.TargetProperty="Source">
                                        <ObjectAnimationUsingKeyFrames>
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="images/repeat-hover.png"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Pressed">
                                    <Storyboard Storyboard.TargetName="bgImg" Storyboard.TargetProperty="Source">
                                        <ObjectAnimationUsingKeyFrames>
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="images/repeat-hover.png"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>
                                <vsm:VisualState x:Name="Disabled">
                                    <Storyboard/>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                            <vsm:VisualStateGroup x:Name="FocusStates">
                                <vsm:VisualState x:Name="Focused">
                                    <Storyboard/>
                                </vsm:VisualState>
                            </vsm:VisualStateGroup>
                        </vsm:VisualStateManager.VisualStateGroups>
                        <Border Height="48" Margin="0,0,0,0" x:Name="Background" Width="48" Background="{x:Null}" CornerRadius="0,0,0,0">
                            <Grid Height="48" Margin="0,0,0,0" Width="48">
                                <Image x:Name="bgImg" Height="48" Margin="0,0,0,0" VerticalAlignment="Bottom" Source="images/repeat.png" Stretch="Fill" Width="48"/>
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:0)

关于可见性变化的

VisualStateManager.GoToState(myControl,“Normal”,true)。 您可以订阅VisibilityChanged事件