user1902345
user1902345

Reputation: 21

WPF textblock blink

I have an application that will need to make the button text flash/blink depending on a dependency object (the sample XAML below uses check box's IsChecked). I've tried many different tricks I've seen on Google search, to no avail.

Below is the sample XAML code I created for this purpose, and there must be something I am missing. Can someone help on this?

=========================

<Window x:Class="WpfTestBed.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="414" Width="751">

    <Window.Resources>
        <Style x:Key="SB_BaseGradientButton" TargetType="Button">
            <Style.Resources>
                <Color x:Key="StartColor">Gray</Color>
                <Color x:Key="EndColor">White</Color>

                <SolidColorBrush x:Key="PressedForegroundColor" Color="Black"/>
            </Style.Resources>
            <Setter Property="Margin" Value="2,2,2,2" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="14" />
        </Style>

        <Storyboard x:Key="FlashBlockTextStoryBoard">
            <ObjectAnimationUsingKeyFrames RepeatBehavior="Forever" Storyboard.TargetName="txtButtonFace" Storyboard.TargetProperty="(UIElement.Visibility)">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{x:Static Visibility.Hidden}" />
                <DiscreteObjectKeyFrame KeyTime="0:0:1"   Value="{x:Static Visibility.Visible}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>

        <Style BasedOn="{StaticResource {x:Type TextBlock}}" x:Key="FlashTextBlock" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=checkBox1.IsChecked}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Name="storyboard" Storyboard="{StaticResource FlashBlockTextStoryBoard}" />
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="storyboard" />
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>


    </Window.Resources>

    <Grid Margin="10,10,10,10" >
        <Grid.RowDefinitions>
            <RowDefinition Height="40*"/>
            <RowDefinition Height="12*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="12*" MaxWidth="210"/>
            <ColumnDefinition Width="20*"/>
        </Grid.ColumnDefinitions>

        <Button Grid.Column="1" MaxHeight="70" MaxWidth="150" 
                FontSize="16" Style="{StaticResource SB_BaseGradientButton}" Margin="320,0,29,12" Grid.Row="1">
            <TextBlock Name="txtButtonFace" TextAlignment="Center" Text="This Is Test" 
                               Style="{StaticResource FlashTextBlock}">
            </TextBlock>
        </Button>
        <CheckBox Content="Make button flash" Grid.Column="1" Height="16" HorizontalAlignment="Left" Margin="44,24,0,0"
                  Name="checkBox1" VerticalAlignment="Top" Grid.Row="1" />
    </Grid>
</Window>

Upvotes: 2

Views: 2382

Answers (1)

LPL
LPL

Reputation: 17083

Two things and it will work:

  • your binding syntax is wrong. It has to be

    Binding="{Binding Path=IsChecked, ElementName=checkBox1}"
    
  • and remove Storyboard.TargetName="txtButtonFace" from your Storyboard because you can't use a TargetName in a Style.

Upvotes: 1

Related Questions