将颜色转换添加到新添加的Listview项目
问题描述:
我已经在windows 10下编写了一个windows通用应用程序,它具有ListView
。将颜色转换添加到新添加的Listview项目
这个ListView
如果新数据可用,每五秒更新一次。它的数据源是ObservableCollection
,最多只能显示10个项目,最新插入到集合的前面。这似乎很有效,因为您看到ListView
项目缓慢地向下滚动屏幕。
我想要做的是向ListView
中的新项目添加某种颜色过渡,以便当它们出现时,该项目的背景从灰色开始并淡入到白色。我想要这种效果,以便用户可以轻松看到刚出现在ListView
中的新项目或项目。
添加到集合中的新对象有一个标志设置为表示它们是新的。如果动画过程能够在动画之后重置此标志,我认为这可以用作指示符?或者我应该使用ListView
以外的事件,如果有的话?
我是新来的故事板,所以我不知道最好的办法。任何人都可以就我应该研究的领域提供建议,甚至可以在UWP下进行研究?
答
基本上,无论何时添加新项目,您都希望将其颜色设置为浅灰色,然后将其右后移动。
因此,关键是要找到在项目容器创建期间调用的事件。在这种情况下,ContainerContentChanging是你的朋友。
由于在动画过程中需要动画几次,所以需要ColorAnimationUsingKeyFrames
而不是普通的ColorAnimation
。整个Timeline
和Storyboard
语法有时会有点混乱,所以我在这里为你创建了一个简单的演示。希望能帮助到你。 :)
private void OnListViewContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.ItemContainer != null && !args.InRecycleQueue && args.Phase == 0)
{
var colorAnimation = new ColorAnimationUsingKeyFrames
{
// 'cause the new item comes in with an animation of which duration is about 300s, we add a little delay here to only
// animate the color after it appears.
BeginTime = TimeSpan.FromMilliseconds(300)
};
var keyFrame1 = new LinearColorKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = Colors.White };
var keyFrame2 = new LinearColorKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(400)), Value = Colors.LightGray };
var keyFrame3 = new LinearColorKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(1200)), Value = Colors.White };
colorAnimation.KeyFrames.Add(keyFrame1);
colorAnimation.KeyFrames.Add(keyFrame2);
colorAnimation.KeyFrames.Add(keyFrame3);
Storyboard.SetTarget(colorAnimation, args.ItemContainer);
Storyboard.SetTargetProperty(colorAnimation, "(Control.Background).(SolidColorBrush.Color)");
var storyboard = new Storyboard();
storyboard.Children.Add(colorAnimation);
storyboard.Begin();
}
}
下面是它在demo应用程序中的外观。
这正是我所需要的。在这方面多读点时间,以便我能更好地理解。 –