Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

datatrigger - WPF Animation Only Firing Once

I have a small ellipse that I want to flash every time a dependency property gets set to true. Because the property can very quickly go from true back to false in a matter of milliseconds, I need to do this with an animation and not a simple style datatrigger. Basically, I just want the true value to ping an animation on the ellipse.

<Ellipse Height="10" Width="10" Stroke="#FFFFFFFF" Margin="5,3,0,0">
    <Ellipse.Fill>
        <SolidColorBrush />
    </Ellipse.Fill>
    <Ellipse.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsReceiving}" Value="True" >
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Fill.Color">
                                    <ColorAnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0.25" Value="Transparent"/>
                                    </ColorAnimationUsingKeyFrames.KeyFrames>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Ellipse.Style>
</Ellipse>

This animation seems to work, but it only fires the first time the value reaches true. Am I missing something?

UPDATE: Thanks for the input everybody. It turns out, it was a threading issue. Originally, I had a DP on the control that was bound to a view model that implemented INotifyPropertyChanged. I then tried removing the DP on the control and turning my view model property into a DP. Boom, that's when I started getting an error stating that a different thread owned the object. I realized I needed to incorporate some Observables using Reactive Extensions as I had done in other parts of the app. I reverted back to the view model traditional property with PropertyChanged() and simply bound that to the control's animation. Everything is working flawlessly now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar problem where I had an 'in' and 'out' animation to be fired when a DP changed. The first cycle worked fine but after that nothing happened. I solved it by adding RemoveStoryboard EnterActions before the BeginStoryboard actions.

<Border Name="Zyzzyx" Grid.Row="1" Background="DarkRed" Height="0" VerticalAlignment="Bottom">
    <Border.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectionState, ElementName=ControlRoot, Mode=OneWay}" Value="Selecting">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="AnimateIn" />
                        <RemoveStoryboard BeginStoryboardName="AnimateOut" />
                        <BeginStoryboard Name="AnimateIn" HandoffBehavior="SnapshotAndReplace">
                            <Storyboard FillBehavior="HoldEnd" Duration="0:0:5">
                                <DoubleAnimation To="30" Duration="0:0:5" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=SelectionState, ElementName=ControlRoot, Mode=OneWay}" Value="Deselecting">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="AnimateIn" />
                        <RemoveStoryboard BeginStoryboardName="AnimateOut" />
                        <BeginStoryboard Name="AnimateOut" HandoffBehavior="SnapshotAndReplace">
                            <Storyboard FillBehavior="HoldEnd" Duration="0:0:5">
                                <DoubleAnimation To="0" Duration="0:0:5" Storyboard.TargetProperty="Height" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>

<!-- Border content -->

</Border>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...