WPF ColorAnimation似乎保留了部分原始颜色?
我在彩色动画方面遇到了一个奇怪的问题。
我有一个绑定到列表框的项目列表。这些项目有一个枚举属性,可以具有三个值之一 - NoRemarks、RemarksFound 和 RemarksUpdated。
在此列表框的数据模板上,我有一个矩形,它显示与项目状态相关的颜色 - 红色、橙色或绿色。模板中有一个数据触发器可以为盒子设置动画,但每当动画触发时,它似乎都会保留旧的颜色。如果我将动画从红色变为绿色,我会得到一种橙色的深绿色。如果我从橙色到绿色设置动画,我会得到类似的颜色,尽管我可以看到颜色与从红色到绿色设置动画时略有不同。
这是 XAML - 我只包含必要的部分:
矩形:
<Rectangle x:Name="HeaderRemarkStatusRectangle" Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" HorizontalAlignment="Left"
RadiusX="1" RadiusY="1" Width="10" Margin="3" StrokeThickness="1" >
<Rectangle.Stroke>
<SolidColorBrush Color="#FFDC5A5A" x:Name="RectangleStroke" />
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#ff7a7a" Offset="0.5" x:Name="GradientStop1" />
<GradientStop Color="#ff5653" Offset="0.5" x:Name="GradientStop2" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
触发器:
<DataTrigger Binding="{Binding Path=ItemStatus}" Value="RemarksUpdated">
<DataTrigger.EnterActions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard Duration="0:0:0.5" >
<ColorAnimation Storyboard.TargetName="GradientStop1" Storyboard.TargetProperty="Color" To="#83ec71" />
<ColorAnimation Storyboard.TargetName="GradientStop2" Storyboard.TargetProperty="Color" To="#5dda4e" />
<ColorAnimation Storyboard.TargetName="RectangleStroke" Storyboard.TargetProperty="Color" To="#FF50AA46" />
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
我尝试了一些方法来纠正问题...
我尝试为每个触发器设置 Datatrigger.exitactions 以将颜色返回为白色(有点忽悠,但我想看看缺少的“From”部分是否是问题)。当动画触发时,我最终得到了一个半透明的盒子,尽管它的颜色是正确的。
我还尝试过设置“from”属性 - 再次,不太实用,因为我不知道触发器的原始颜色是什么,但我想看看效果。我得到了与上面类似的结果。
还尝试更改 FillBehaviour 属性,但将其设置为“停止”意味着动画完成后颜色将恢复为原始颜色(如预期)。
我认为动画将从当前颜色(从数据模板或当前动画(如果存在))开始动画,并以新颜色开始动画。我不太明白为什么我似乎得到了原始颜色和新颜色之间的颜色。这是彩色动画的已知问题还是我的触发器的问题?
谢谢大家
I'm having a strange problem with a colour animation.
I have a list of items bound to a listbox. These items have an enum property which can have one of three values - NoRemarks, RemarksFound and RemarksUpdated.
On the datatemplate for this listbox, I have a rectangle which displays a colour related to the item status - Red, Orange or Green. There is a data trigger in the template to animate the box, but whenever the animation fires, it appears to sort of retain the old colours. If I animate from red to green, I get a sort of orangy dark green colour. If I animate from orange to green, I get a similar colour, although I can see that the colour is slightly different than when I animate from red to green.
Here is the XAML - I've only included the necessary parts:
The rectangle:
<Rectangle x:Name="HeaderRemarkStatusRectangle" Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" HorizontalAlignment="Left"
RadiusX="1" RadiusY="1" Width="10" Margin="3" StrokeThickness="1" >
<Rectangle.Stroke>
<SolidColorBrush Color="#FFDC5A5A" x:Name="RectangleStroke" />
</Rectangle.Stroke>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#ff7a7a" Offset="0.5" x:Name="GradientStop1" />
<GradientStop Color="#ff5653" Offset="0.5" x:Name="GradientStop2" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
The triggers:
<DataTrigger Binding="{Binding Path=ItemStatus}" Value="RemarksUpdated">
<DataTrigger.EnterActions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard Duration="0:0:0.5" >
<ColorAnimation Storyboard.TargetName="GradientStop1" Storyboard.TargetProperty="Color" To="#83ec71" />
<ColorAnimation Storyboard.TargetName="GradientStop2" Storyboard.TargetProperty="Color" To="#5dda4e" />
<ColorAnimation Storyboard.TargetName="RectangleStroke" Storyboard.TargetProperty="Color" To="#FF50AA46" />
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
I have tried a couple of things to correct the problem...
I have tried setting the Datatrigger.exitactions for each trigger to return the colour to white (a bit of a fudge but I wanted to see if the missing "From" section was the problem). When the animation triggered, I ended up with a semi-transparent box, although it was the right colour.
I have also tried setting the "from" property - again,not really practical as I don't know what the original colour for the trigger would be but I wanted to see the effect. I got a similar result as above.
Also tried changing the FillBehaviour property, but setting it to "stop" meant that the color would revert to the original colour as soon as the animation had completed (as expected).
I thought that the animation would animate from the current colour (either from the datatemplate or from the current animation if one exists), and would animate to the new colour. I can't quite suss out why I seem to get a colour inbetween the original and new colour. Is this a known problem with the colour animation or is it a problem with my triggers?
Thanks all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置 ColorAnimation 对象的持续时间。默认的持续时间动画时长为 1 秒,因此 0.5 秒的故事板仅运行动画的一半。
You need to set the Duration on the ColorAnimation objects. The default Duration for Animations is 1 second, so your 0.5 second storyboard is only running half of the animation.