开发者

ListView CustomRoutedEvent to Trigger Animation on row

Hi Guys I have a listview bound to an observableCollection, when a new item comes in, it flashes that row.

so Far I have a event trigger in my itemcontainer style..

<Style TargetType="WpfApplication2:CustomListViewItem">
    <Style.Triggers>
        <EventTrigger RoutedEvent="<My Custom Routed Event>">
            <BeginStoryboard>
                <Storyboard >
                    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"
                                    From="Navy" 
                                    To="White"
                                    Duration="0:0:0.4"
                                    AutoReverse="True"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>                   
</Style>

And also a custom routed event that that I want to fire w开发者_StackOverflowhen a new item is added. Finding it difficult to understand where I should put this routed event and how to fire it.

Thanks


I do not think this belongs into the ItemContainerStyle since the item management is at the higher level of the ListView itself.

One way you can approach this is by using a behavior from the Blend SDK, e.g.:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<ListView ItemsSource="{Binding DpData}">
    <i:Interaction.Behaviors>
        <b:FlashNewRowBehavior />
    </i:Interaction.Behaviors>
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn DisplayMemberBinding="{Binding Occupation}" />
        </GridView>
    </ListView.View>
</ListView>
class FlashNewRowBehavior : Behavior<ListView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Loaded += new System.Windows.RoutedEventHandler(AssociatedObject_Loaded);
    }

    void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var itemsSource = AssociatedObject.ItemsSource;
        if (itemsSource is INotifyCollectionChanged)
        {
            var collection = itemsSource as INotifyCollectionChanged;
            collection.CollectionChanged += (s, cce) =>
                {
                    if (cce.Action == NotifyCollectionChangedAction.Add)
                    {
                        // This code sadly is rather unclean, some wait is necessary or the ItemContainerGenerator will return null
                        Wait(TimeSpan.FromSeconds(0.01));

                        var itemContainer = AssociatedObject.ItemContainerGenerator.ContainerFromItem(cce.NewItems[0]) as ListViewItem;
                        if (itemContainer != null)
                        {
                            Storyboard sb = new Storyboard();
                            ColorAnimation anim = new ColorAnimation()
                            {
                                // From is not really needed (if you want to animate from the current background color at least)
                                From = Colors.Navy,

                                // I would create properties instead of hardcoded values
                                To = Colors.White,
                                Duration = (Duration)TimeSpan.FromSeconds(0.4),

                                AutoReverse = true
                            };
                            Storyboard.SetTargetProperty(anim, new PropertyPath("Background.Color"));
                            Storyboard.SetTarget(anim, itemContainer);
                            sb.Children.Add(anim);
                            sb.Begin(); 
                        }
                    }
                };
        }
    }

    private static void Wait(TimeSpan timeout)
    {
        var frame = new DispatcherFrame();
        new Thread((ThreadStart)(() =>
        {
            Thread.Sleep(timeout);
            frame.Continue = false;
        })).Start();
        Dispatcher.PushFrame(frame);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜