我模拟了一个不断更新和排序的汽车视频游戏记分牌。一切都按照我的预期工作,但后来我尝试添加一个EventTrigger,当新的驱动程序插入我的列表并产生奇怪的行为时,它会制作动画。
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<!--...-->
</EventTrigger>
有时当我在一辆汽车中发射该事件时,似乎有更多的汽车被怀疑参与此事件,因此不会像我预期的那样。显然我希望每辆车都有他自己的活动。此外,当记分牌被分类时,即当汽车改变其位置时,也会触发该事件。
现在我要解释一下我的内容:
以下列表会不断更新(添加和更新元素),并且不按位置排序,因为我直接在我的视图中执行此操作:
public ObservableCollection<Car> ListaObservable { get; set; }
从视图中提取以下代码:
<UserControl>
<UserControl.Resources>
<DataTemplate DataType="models:Car">
<!-- Each grid represents a car that should shine everytime it passes through the finish line -->
<Grid>
<!--...-->
<Grid.Triggers>
<!-- Everytime "Estimacion" is updated, that event is fired as I expected -->
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard >
<!-- It doesn't matter what kind of animation is -->
<Duration="{Binding Estimacion, NotifyOnTargetUpdated=True}"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- The event I mentioned before is never fired when I add a new car to my list of Cars so I created that event but sometimes it does strange behaviours -->
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<!-- ... -->
</BeginStoryBoard>
</Eventrigger>
</Grid.Triggers>
</Grid>
</DataTemplate>
<CollectionViewSource x:Key="SortedItems" Source="{Binding ListaObservable}" IsLiveSortingRequested="True">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Posicion"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<Grid>
<ListBox Name="ListBox" ItemsSource="{Binding Source={StaticResource SortedItems}}" BorderThickness="0" MinHeight="800" />
</Grid>
</UserControl>
最后,在我的情况下,如何通过我的ViewModel直接触发EventTrigger?
如何创建可以随时触发的新事件?