I have a WPF TextBlock. TextBlock is bound to a MVVM boolean property. I am trying to make it visible or not and blinking depending on this MVVM boolean property value. If it is true, I make it visible and show TextBlock blinking (I start storyboard animation), otherwise, I make it not visible and not blinking and I stop storyboard animation. Below the code not working. What is the problem?
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
<Storyboard x:Key="BlinkingAnimation" Duration="0:0:1" RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="Black" />
<DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="Red" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">
<DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red" />
<DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="Black" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Style x:Key="BlinkingAnimationStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=BlinkOn}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="BlinkingAnimation_BeginStoryboard" Storyboard="{StaticResource BlinkingAnimation}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="BlinkingAnimation_BeginStoryboard" />
</DataTrigger.ExitActions>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=BlinkOn}" Value="false">
<DataTrigger.EnterActions>
<RemoveStoryboard BeginStoryboardName="BlinkingAnimation_BeginStoryboard" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Border Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}" BorderThickness="1" BorderBrush="Red" CornerRadius="5" Margin="5">
<TextBlock x:Name="lblStoryboard"
Padding="5"
Width="480"
Style="{StaticResource BlinkingAnimationStyle}"
Text="Hey there! I am Blinking!!!"
TextWrapping="WrapWithOverflow"
Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}">
</TextBlock>
</Border>
View Model:
private bool _blinkOn = false;
public bool BlinkOn
{
get
{
return _blinkOn;
}
set
{
if (_blinkOn== value) return;
_blinkOn= value;
OnPropertyChanged("BlinkOn");
}
}
AlertOn
andBlinkOn
and 2) for binding to a view-model property, you have to use the data context as binding source, notRelativeSource.Self
– dymanoid