开发者

How to reset a silverlight control's visual state back to "Normal" on change of visibility

I have a problem with a button control with rolloever effect. I am collapsing the button on click a开发者_运维问答nd later it becomes visible again, but the Mouse Hover state doesn't change back to Normal when the button becomes visible. However the issue can be resolved by changing the visual state in code but I w'd like to know if there is a way to do it in the control template? Here is my template:

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


VisualStateManager.GoToState(myControl, "Normal", true) on visibility change. You can subscribe to the VisibilityChanged event

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜