Search code examples
wpfexpression-blendvisualstatemanagervisualstatesrepeatbutton

WPF RepeatButton with Pressed VisualState


1) my repeat button visual state is a rectangle which Stroke go's from Transparent to Gray when pressed,

this visual state change occurs only once on Pressed,

since this is a repeat button i would like the visual state change to re occur (like a blinking pressed) over and over while pressing , how could i change my visual state to get such an effect

<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) One approach i had in mind is to use GoToStateAction with an EventTrigger on Click event (since the Repeat button re fire that event over and over) ,

but i can't seem to place a GoToStateAction directly on the ControlTemplate , and hadn't had much luck placing it under and EventTrigger under the ControlTemplate .

So to Conclude iv'e got 2 issues :

1) A general idea how to solve this issue .

2) My idea requires my to place a GoToStateAction on a ControlTemplate Object , it seems that this can't be done , any ideas how to work around this ?

thanks in advance.


Solution

  • Try using Triggers instead of Visual States

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