带数据绑定的 WPF TextBlock 背景动画
WPF TextBlock Background Animation w/ Data Binding
当数据绑定布尔值 属性 设置为 'True' 时,我一直在尝试将 TextBlock 背景设置为从白色变为红色然后再变为红色(闪烁)的动画。每次我 运行 它都会进入中断模式。
我知道我错过了什么。如果您能提供任何帮助或指导,我们将不胜感激。
提前致谢!
<TextBlock x:Name="txtItemDisplayText" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ItemCountText}" VerticalAlignment="Top" Height="600" Width="800" TextAlignment="Center" FontSize="525" FontWeight="Bold" FontFamily="Agency FB" Foreground="Green">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Private mBlink As Boolean = False
Public Property Blink() As Boolean
Get
Return mBlink
End Get
Set(value As Boolean)
mBlink = value
RaisePropertyChanged("Blink")
End Set
End Property
您不能为预定义画笔的颜色 属性 设置动画。为显式设置新 SolidColorBrush 实例的背景 属性 添加 Setter:
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
当数据绑定布尔值 属性 设置为 'True' 时,我一直在尝试将 TextBlock 背景设置为从白色变为红色然后再变为红色(闪烁)的动画。每次我 运行 它都会进入中断模式。
我知道我错过了什么。如果您能提供任何帮助或指导,我们将不胜感激。
提前致谢!
<TextBlock x:Name="txtItemDisplayText" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ItemCountText}" VerticalAlignment="Top" Height="600" Width="800" TextAlignment="Center" FontSize="525" FontWeight="Bold" FontFamily="Agency FB" Foreground="Green">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Private mBlink As Boolean = False
Public Property Blink() As Boolean
Get
Return mBlink
End Get
Set(value As Boolean)
mBlink = value
RaisePropertyChanged("Blink")
End Set
End Property
您不能为预定义画笔的颜色 属性 设置动画。为显式设置新 SolidColorBrush 实例的背景 属性 添加 Setter:
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="White"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Blink}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sbBlink">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="White" To="Red" FillBehavior="Stop"
BeginTime="0:0:0" Duration="0:0:0.3"/>
<ColorAnimation Storyboard.TargetProperty="Background.Color"
From="Red" To="White"
BeginTime="0:0:0.3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sbBlink"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>