2011-06-04 59 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); 
    } 
}