2012-12-13 118 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