With the following definitions, OnSignalChanged
never gets executed when Signal
in the INotifyPropertyChanged-compliant view model changes its value and, of course, the animation doesn't play. Am I misunderstanding something fundamental? TestCommand
simply sets Signal
from false to true.
Public Class StoryboardSignalManager Public Shared SignalProperty As DependencyProperty = DependencyProperty.RegisterAttached("Signal", GetType(Boolean), GetType(StoryboardSignalManager), New PropertyMetadata(AddressOf OnSignalChanged)) Public Shared Function GetSignal(d As DependencyObject) As Boolean? Return DirectCast(d.GetValue(SignalProperty), Boolean?) End Function Public Shared Sub SetSignal(d As DependencyObject, value As Object) d.SetValue(SignalProperty, value) End Sub Private Shared Sub OnSignalChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs) Dim value As Boolean = DirectCast(e.NewValue, Boolean) Dim storyboard As Storyboard = DirectCast(d, Storyboard) If value Then storyboard.Begin() Else storyboard.Stop() End If End Sub End Class
<Grid>
<Grid.Resources>
<Storyboard x:Key="Fade" local:StoryboardSignalManager.Signal="{Binding Signal}">
<DoubleAnimationUsingKeyFrames Storyboard.Target="{Binding ElementName=TextBlock}" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="00:00:00.00" Value="1" />
<EasingDoubleKeyFrame KeyTime="00:00:01.00" Value="0" />
<EasingDoubleKeyFrame KeyTime="00:00:02.00" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<StackPanel>
<TextBlock x:Name="TextBlock" Text="test" />
<Button Width="100" Height="50" Command="{Binding TestCommand}" />
</StackPanel>
</Grid>