2

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

下面是我为此目的创建的示例 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>
4

1 回答 1

1

两件事,它将起作用:

  • 您的绑定语法错误。它一定要是

    Binding="{Binding Path=IsChecked, ElementName=checkBox1}"
    
  • Storyboard.TargetName="txtButtonFace"从您的中删除,Storyboard因为您不能TargetName在 a 中使用 a Style

于 2012-12-13T23:37:35.690 回答