Sorting DataGrid while animating cells causes exception
I have created an DataGridCell template that I have applied to one of my columns like so:
<DataGridTextColumn Binding="{Binding LastUpdated}"
IsReadOnly="True" CanUserReorder="False"
CanUserSort="False" CanUserResize="False"
CellStyle="{StaticResource DataGridCellStyle1}"
/>
And the template looks like this:
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ControlTemplate.Resources>
<Storyboard x:Key="CellChangedStoryboard">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="DGC_Border">
<EasingColorKeyFrame KeyTime="0" Value="LightGreen"/>
<EasingColorKeyFrame KeyTime="0:0:8" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="DGC_Border" Background="Red">
<ContentPresenter />
<i:Interaction.Triggers>
<ei:TimerTrigger MillisecondsPerTick="1000" >
<ei:ControlStoryboardAction Storyboard="{StaticResource CellChangedStoryboard}"/>
</ei:TimerTrigger>
</i:Interaction.Triggers>
</Border>
</ControlTemplate>
In this example I am triggering the anim using a Blend TimerTrigger, but it doesnt matter how I trigger my animation, it always crashes as soon as I try to sort my DataGrid (by clicking on one of the column headers)
If I remove the storyboard then it does not crash on sort.
The exception is InvalidOperationException: 'DGC_Border' name cannot be found in the name scope of 'System.Windows.Controls.Border'.
I suspect that when the DataGrid sorts it destroys or otherwise messes with the cells/columns and the animation which is running can no longer find the cell it was animating.
Why is the exception happening and how do I stop it? Thanks
Update: Seems to work Ok if I use <ControlTemplate.Triggers>
to trigger the StoryBoard. Unfortu开发者_运维知识库nately this is no solution as I need to play the storyboard when a bound property changes.
Best I could come up with was to ditch the Blend triggers and use this:
<Border x:Name="DGC_Border" Background="Red">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Content="{Binding LastUpdated, NotifyOnTargetUpdated=True}">
<ContentPresenter.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard Storyboard="{StaticResource RowChangedStoryboard}" />
</EventTrigger>
</ContentPresenter.Triggers>
</ContentPresenter>
</Border>
I notice that after the grid sorts all the animations are removed, which is not correct behavior for me - I want the animations to continue from exactly where they left off but I will have to save that for another day.
精彩评论