在我的ObservableCollection
中添加新项目会触发动画的最后一行(由于排序,新行位于顶部),还滚动并调整了所有行的动画大小,这些行之前并没有再次显示。
添加后,我只需要对每行添加一次动画。
我认为我正在努力寻找合适的(事件?)触发器,却不知道如何正确处理排序。有什么建议吗?
<UserControl.Resources>
<CollectionViewSource IsLiveSortingRequested="True" Source="{Binding Incidents}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="TimeStamp" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<DockPanel Margin="0">
<Label DockPanel.Dock ="Top" FontWeight="Black">
Incidents:
</Label>
<Grid DockPanel.Dock ="Bottom">
<DataGrid Background="{x:Null}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.PanningMode="VerticalOnly"
ItemsSource="{Binding Source={StaticResource cvs}}"
AutoGenerateColumns="False" IsReadOnly="True"
CanUserSortColumns="True" HeadersVisibility="All" FontSize="12"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Binding="{Binding TimeStamp, StringFormat={}{0:HH:mm:ss}}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding CarName}"></DataGridTextColumn>
<DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(DataGridRow.Background).(SolidColorBrush.Color)"
Duration="00:00:08"
From="Red" To="Transparent"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
</DockPanel>
public class Incident
{
public string CarName { get; set; }
public DateTime TimeStamp { get; set; }
public string Message { get; set; }
}
public class IncidentCollection : ObservableCollection<Incident>
{
}