WPF文本块闪烁

时间:2012-12-13 22:56:15

标签: c# wpf animation textblock

我有一个应用程序,需要根据依赖项对象使按钮文本闪烁/闪烁(下面的示例XAML使用复选框的IsChecked)。我尝试了许多我在谷歌搜索上看到的不同技巧,但无济于事。

以下是我为此目的创建的示例XAML代码,并且必须存在我遗漏的内容。有人可以为此提供帮助吗?

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

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

1 个答案:

答案 0 :(得分:1)

两件事,它会起作用:

  • 您的绑定语法错误。它必须是

    Binding="{Binding Path=IsChecked, ElementName=checkBox1}"
    
  • 并从Storyboard.TargetName="txtButtonFace"删除Storyboard,因为您无法在TargetName中使用Style