( 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)我想到的一种方法是在单击事件上使用GoToStateAction和EventTrigger (因为重复按钮会一次又一次地触发该事件),
但我似乎无法将GoToStateAction直接放置在ControlTemplate上,也不太幸运地将它放置在ControlTemplate下面,并将EventTrigger放在ControlTemplate下面。
因此总结出两个问题:
( 1)解决这一问题的总体思路。
2)我的想法要求我把一个GoToStateAction放在一个ControlTemplate对象上,这似乎是做不到的,有什么想法可以解决这个问题吗?
提前谢谢。
发布于 2013-08-12 03:37:11
尝试使用触发器而不是可视化状态
<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>https://stackoverflow.com/questions/18169897
复制相似问题