如何在添加新项目的特定时间后为每个ListViewItem使用ColorAnimation

时间:2016-04-18 05:04:28

标签: c# xaml win-universal-app listviewitem coloranimation

我正在使用Windows 10应用。我使用了ListView,它通过文本框获取其项目。我需要在一段时间后使ListViewItems闪烁(或简单地改变颜色)。我使用了DispatcherTimer。当我运行应用程序时,会执行故事板,但只有标题闪烁而不是ListView。任何有关这方面的帮助将不胜感激。

这是XAML部分:

<Page
x:Class="listViewAnimation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:listViewAnimation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <Storyboard x:Name="Storyboard1">
        <ColorAnimation Storyboard.TargetName="listView" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
                                        AutoReverse="True" RepeatBehavior="Forever"/>
    </Storyboard>
</Page.Resources>

<Grid Background="Black">
    <ListView x:Name="listView" HorizontalAlignment="Left" Height="566" Margin="428,128,0,0" VerticalAlignment="Top" Width="1357" Foreground="#FFF90000" Header="blah blah">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" >
                <Style.Setters>
                    <Setter Property="FontSize" Value="370"/>
                    <Setter Property="FontFamily" Value="Digital-7 Mono"/>
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="Width" Value="630"/>


                </Style.Setters>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsStackPanel
                    Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>


    </ListView>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="369,899,0,0" TextWrapping="Wrap" Text="14" VerticalAlignment="Top" Height="60" Width="194"/>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="631,925,0,0" VerticalAlignment="Top" Background="#FFFBFBFE" BorderBrush="#FFFBFBFE" Click="button_Click"/>
</Grid>

背后的代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace listViewAnimation
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        DispatcherTimer t = new DispatcherTimer();
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            listView.Items.Add(textBox.Text);
           manip();
        }

        private void manip()
        {

            t.Interval = new TimeSpan(0, 0, 2);
            t.Tick += T_Tick;
            t.Start();
        }

        private void T_Tick(object sender, object e)
        {
            Storyboard1.Begin();
        }
    }
}

注意:此外,我是Windows 10开发的新手。

1 个答案:

答案 0 :(得分:0)

您必须将Target属性设置为已添加ListViewItem而不是ListView

 <Storyboard x:Name="Storyboard1">
            <ColorAnimation  Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" From="Red" To="YellowGreen" Duration="0:0:0.2"
                                        AutoReverse="True" RepeatBehavior="Forever"/>
        </Storyboard>

 ListViewItem item;
        private void button_Click(object sender, RoutedEventArgs e)
        {
            listView.Items.Add(textBox.Text);
            listView.UpdateLayout();
         item =  (ListViewItem) listView.ContainerFromIndex(listView.Items.Count-1);
            manip();
        }

    private void T_Tick(object sender, object e)
            {
                if (item != null)
                {
                  Storyboard1.Stop();//If animation is running for previously added item
                  Storyboard.SetTarget(Storyboard1, item);
                  Storyboard1.Begin();
                    t.Stop();
                }
            }