2011-06-04 70 views
0

嗨,夥計們我有一個listview綁定到一個observableCollection,當一個新的項目進來時,它閃爍該行。ListView CustomRoutedEvent觸發行上的動畫

到目前爲止,我有一個事件觸發我itemcontainer風格..

<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> 

而且也是我想在添加一個新的項目,以觸發一個自定義的路由事件。 發現很難理解我應該把這個路由事件放在哪裏,以及如何解僱它。

感謝

回答

0

我不認爲這屬於進ItemContainerStyle由於項目管理是ListView控件本身的更高的水平。你可以接近這個

一種方式是通過使用來自Blend SDK行爲,例如:

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); 
    } 
}