首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带压下RepeatButton的WPF VisualState

带压下RepeatButton的WPF VisualState
EN

Stack Overflow用户
提问于 2013-08-11 07:52:18
回答 1查看 874关注 0票数 0

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

此可视状态更改只发生一次按下,

由于这是一个重复按钮,我希望视觉状态改变在按压时反复发生(就像眨眼按下),我如何才能改变我的视觉状态以获得这样的效果?

代码语言:javascript
复制
<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)我想到的一种方法是在单击事件上使用GoToStateAction和EventTrigger (因为重复按钮会一次又一次地触发该事件),

但我似乎无法将GoToStateAction直接放置在ControlTemplate上,也不太幸运地将它放置在ControlTemplate下面,并将EventTrigger放在ControlTemplate下面。

因此总结出两个问题:

( 1)解决这一问题的总体思路。

2)我的想法要求我把一个GoToStateAction放在一个ControlTemplate对象上,这似乎是做不到的,有什么想法可以解决这个问题吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-12 03:37:11

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

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18169897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档