1

我有一个DataGrid包含一个项目列表,该列表实现了一个名为 的布尔属性ResultChanged,该属性允许我在 的特定单元格上创建闪烁效果,DataGrid特别是,如果该属性设置为,true则该单元格DataGrid将被橙色着色并闪烁 5 秒钟,在动画结束时,颜色将保持不变,DataGrid这对我来说效果很好,除了一个问题。

问题

当我单击DataGrid(任何行)时,动画应用的颜色将被删除,但我希望这种颜色保留在单元格上。

这是我的代码:

<DataGridTextColumn Header="{DynamicResource hour}" Binding="{Binding Result}">
<DataGridTextColumn.CellStyle>
  <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource MaterialDesignDataGridCell}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ResultChanged}" Value="True" >
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <Storyboard x:Name="Blink" 
                                    AutoReverse="True" 
                                    RepeatBehavior="5x">
                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
                                Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="00:00:01" 
                                                     Value="Orange" />
                            </ColorAnimationUsingKeyFrames>
                            <ColorAnimationUsingKeyFrames 
                                BeginTime="00:00:00"
                                Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="00:00:01" 
                                                     Value="Black" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                        <Storyboard AutoReverse="False">
                            <ColorAnimationUsingKeyFrames BeginTime="00:00:10" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="00:00:01" Value="Orange" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
  </Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>

有人知道我做错了什么吗?

4

1 回答 1

1

当您选择或取消选择受动画影响的行时,动画的效果将丢失,因为DataGrid更改了所选行的前景和背景画笔。现在,通常情况下,动画对ForegroundBackground属性的更改将优先于所选行的更改。但是您的动画没有动画Foreground并且Background!相反,它动画Foreground.ColorBackground.Color; Foreground这些更改没有优先级,并且会在其他更改或Background直接更改时丢失。

因此,您的动画必须具有动画效果Foreground并且是Background直接的。然后,即使在动画结束后,更改仍将保持有效。

例如,您可以通过使用有问题的Brush to Brush AnimationBrushAnimation答案中的类并使用与此类似的情节提要来实现此目的:

<DataTrigger Binding="{Binding ResultChanged}" Value="True" >
    <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <Storyboard x:Name="Blink" 
                            RepeatBehavior="5x">
                    <local:BrushAnimation Storyboard.TargetProperty="Background" 
                                            BeginTime="00:00:00" Duration="0:0:0.5" From="White" To="Orange" />
                    <local:BrushAnimation Storyboard.TargetProperty="Foreground" 
                                            BeginTime="00:00:00" Duration="0:0:0.5" From="Yellow" To="Black" />
                </Storyboard>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>
</DataTrigger>

编辑:

这是一个允许AutoReverse设置为 true 的解决方案:

<DataTrigger Binding="{Binding ResultChanged}" Value="True" >
    <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <!-- The next two animations will take 10 seconds: -->
                <Storyboard x:Name="Blink" AutoReverse="True" RepeatBehavior="5x">
                    <local:BrushAnimation Storyboard.TargetProperty="Background" 
                                            BeginTime="00:00:00" Duration="0:0:1" From="White" To="Orange"/>
                    <local:BrushAnimation Storyboard.TargetProperty="Foreground" 
                                            BeginTime="00:00:00" Duration="0:0:1" From="Yellow" To="Black"/>
                </Storyboard>
                <!-- Same animations as above, but BeginTime offset by 10 seconds and neither repeat nor auto reverse: -->
                <Storyboard x:Name="finalBlink" BeginTime="00:00:10">
                    <local:BrushAnimation Storyboard.TargetProperty="Background" 
                                            BeginTime="00:00:00" Duration="0:0:1" From="White" To="Orange"/>
                    <local:BrushAnimation Storyboard.TargetProperty="Foreground" 
                                            BeginTime="00:00:00" Duration="0:0:1" From="Yellow" To="Black"/>
                </Storyboard>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>
</DataTrigger>
于 2018-07-08T08:39:44.310 回答