带有按下VisualState的WPF RepeatButton

时间:2013-08-11 07:52:19

标签: wpf expression-blend visualstatemanager visualstates repeatbutton

1)我的重复按钮视觉状态是一个矩形,当按下时,笔划从透明变为灰色,

此视觉状态更改仅在按下时发生一次,

因为这是一个重复按钮,我想在按下时反复发生视觉状态变化(如闪烁按下), 我怎么能改变我的视觉状态才能获得这样的效果

<ControlTemplate TargetType="{x:Type RepeatButton}">
    <Grid>
      <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualStateGroup.Transitions>
            <VisualTransition GeneratedDuration="0" To="Pressed"/>
            </VisualStateGroup.Transitions>
               <VisualState x:Name="Pressed">
                  <Storyboard>
                     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
                            <EasingColorKeyFrame KeyTime="0" Value="#FF8F8E8E" />
                    </ColorAnimationUsingKeyFrames>
                  </Storyboard>
               </VisualState>
        </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>
    <Rectangle x:Name="rectangle" HorizontalAlignment="Stretch" Stroke="Transparent" Fill="Transparent" VerticalAlignment="Stretch" />                                                                      
    </Grid>                 
</ControlTemplate>

2)我想到的一种方法是在Click事件中使用GoToStateAction和EventTrigger(因为重复按钮会反复触发该事件),

但是我似乎无法将GoToStateAction直接放在ControlTemplate上,并且没有太多运气将它放在ControlTemplate下面和EventTrigger下。

所以结论我有2个问题:

1)如何解决这个问题的一般想法。

2)我的想法要求我在ControlTemplate对象上放置一个GoToStateAction,似乎这不可能   完成,任何想法如何解决这个问题?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用触发器而不是视觉状态

<ControlTemplate TargetType="{x:Type RepeatButton}">
                            <ControlTemplate.Resources>
                                <Storyboard x:Key="repeatSb" AutoReverse="True" RepeatBehavior="Forever">
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle">
                                        <EasingColorKeyFrame KeyTime="0" Value="Red"   />
                                        <EasingColorKeyFrame KeyTime="0:0:0.5" Value="Transparent"/>
                                </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </ControlTemplate.Resources>
                                <Grid>
                                 <Rectangle x:Name="rectangle" HorizontalAlignment="Stretch" 
                                       Stroke="Transparent" Fill="#FFFBD0D0" VerticalAlignment="Stretch" />                                                                      
                               </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsPressed" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard x:Name="repeatSb_BeginStoryboard" 
                                        Storyboard="{StaticResource repeatSb}"/>
                                    </Trigger.EnterActions>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="False">
                                    <Trigger.EnterActions>
                                        <StopStoryboard BeginStoryboardName="repeatSb_BeginStoryboard"/>
                                    </Trigger.EnterActions>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
相关问题