4

Hi iam trying to make an Wpf TextBlock to blink. I want like when im clicking on an button then the textblock blinks. How can i achive this.

I have tried the following.

<TextBlock Name="txtBlockScannerText" Margin="10,0,0,0" Style="{StaticResource TextBlockNormal}" Text="Skanna Inleverans listan">
    <TextBlock.Triggers>
        <EventTrigger RoutedEvent="TextBlock.MouseEnter">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard BeginTime="00:00:00" RepeatBehavior="Forever" Storyboard.TargetName="txtBlockScannerText" Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                        <ColorAnimation From="Black" To="Red" Duration="0:0:1"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </TextBlock.Triggers>
</TextBlock>

But with this code it only blinks when my mouse enter it. How can i trigger the blink in an button click event. Or how do i call the event to blink. Thanks for help

H.B.
  • 166,899
  • 29
  • 327
  • 400
Tan
  • 2,148
  • 3
  • 33
  • 54

3 Answers3

7

Here is the solution

<TextBlock Name="txtBlockScannerText" Margin="10,0,0,0"  Text="WELCOME"> </TextBlock>
        <Button Content="Click Me" Height="23" HorizontalAlignment="Left" Margin="225,43,0,0" Name="button1" VerticalAlignment="Top" Width="75">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard BeginTime="00:00:00" 
                                        RepeatBehavior="Forever" 
                                        Storyboard.TargetName="txtBlockScannerText" 
                                        Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                                 <ColorAnimation From="Black" To="Blue" Duration="0:0:1"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Button.Triggers>
        </Button>

Chris_O
  • 3,429
  • 1
  • 22
  • 28
5

Make your trigger listen to the Loaded event rather than the MouseEnter event...

<EventTrigger RoutedEvent="TextBlock.Loaded">
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • but now it will blink when the textblock is loaded. How do i make it only blink when i click a button.Thanks – Tan Apr 16 '10 at 12:38
5

There's no click event on a TextBlock. If you use a button with the textblock as content you can hook up your animation to the button's click event. You may need to style the button to remove 3D look or what else you may choose as default style for your buttons.

Liton
  • 1,398
  • 2
  • 14
  • 27
Wallstreet Programmer
  • 9,567
  • 3
  • 37
  • 52
  • So theres no way to trugger the textblock to blink with an button.mm alright thanks for the help guys – Tan Apr 16 '10 at 13:49
  • 1
    oh yes ,thats an solution. i didnt figured that out, Thanks! – Tan Apr 16 '10 at 14:00