2012-12-13 70 views
1

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

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

任何帮助您为WPF重新发明“眨眼”的人都会被我拒绝。 :-) –

+0

更严肃的说明 - 这个XAML生产什么?您是否在调试时查看了输出窗口,以查看是否存在绑定错误。 –

回答

1

两件事情,它会工作:

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

    Binding="{Binding Path=IsChecked, ElementName=checkBox1}" 
    
  • ,并从Storyboard删除Storyboard.TargetName="txtButtonFace",因为你不能在一个Style使用TargetName