WPF DataGrid - 插入数据网格时突出显示新行
我有一个绑定到 ObservableCollection 的数据网格,我想做的是在将新行添加到数据网格时(即,当将新对象插入到 ObservableCollection 中时)突出显示新行。我想在插入行时突出显示行,方法是最初更改背景颜色,然后随着时间的推移颜色逐渐恢复正常。我尝试了各种不同的方法来让它工作,但没有一种方法可以正常工作。
方法 1:我有一个在列加载时触发的事件触发器。当元素加载时它确实会触发,但它似乎也在其他旧行上几乎随机触发(当该行是新行时它已经触发过一次的行)。
<DataGridHyperlinkColumn x:Name="OrderID" Binding="{Binding OrderNumber}" Header="Order" SortMemberPath="ciOrderId">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Transparent"/>
<EventSetter Event="Hyperlink.Click" Handler="OrderNumber_Click" />
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:03"
From="Red" To="Transparent" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
方法 2:我在视图模型中创建了一个布尔值,当新项目添加到 ObservableCollection 时,该布尔值设置为 true。然后,我在触发器中检查该值,如果为真,则触发故事板。但我无法让它正常工作,并且当我运行它时应用程序不断出错。另外,一旦故事板运行,我无法找到将此值设置为 false 的方法(我无法使用故事板的 Completed 事件,因为 DataTrigger 是一种样式)。
<DataTrigger Binding="{Binding isNew}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:03"
From="Red" To="{x:Null}" FillBehavior="Stop"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
方法3:当新项目添加到可观察集合中时,我尝试在视图模型中设置时间戳字段。然后在 XAML 中,我希望能够将该时间戳与当前时间进行比较,如果匹配,那么我将触发该事件。我什至还有另一个包含当前时间并由 INotifyPropertyChanged 自动更新的字段,但我似乎无法找到一种方法来将新行的时间戳与包含当前时间的字段进行比较。
我觉得必须有一个解决方案,但在花了令人沮丧的一天试图解决这个问题之后,我希望有人能够提供一些线索。
I have a datagrid bound to an ObservableCollection, and what I'd like to do is highlight new rows when they are added to the datagrid (i.e. when a new object is inserted into the ObservableCollection). I'd like to highlight the rows when they are inserted by changing the background colour initially, but then having the colour fade back to normal over time. I've tried a variety of different methods to get it to work but nothing quite works properly.
Method 1: I have an event trigger that fires when the column loads. It does fire when the element loads, but it seems to fire almost randomly on other old rows as well (rows on which it already fired once when the row was new).
<DataGridHyperlinkColumn x:Name="OrderID" Binding="{Binding OrderNumber}" Header="Order" SortMemberPath="ciOrderId">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Transparent"/>
<EventSetter Event="Hyperlink.Click" Handler="OrderNumber_Click" />
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:03"
From="Red" To="Transparent" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
Method 2: I made a bool in the view model that is set to true when the new item is added to the ObservableCollection. I then check this value in a trigger and if it is true I fire the storyboard. I can't get this to work properly though, and the application keeps erroring out when I run it. Also, I can't figure out a way to set this value to false once the storyboard has run (I can't use the storyboard's Completed event because the DataTrigger is in a style).
<DataTrigger Binding="{Binding isNew}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:03"
From="Red" To="{x:Null}" FillBehavior="Stop"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
Method 3: I tried setting a timestamp field in the view model when the new item is added to the observable collection. Then in XAML I want to be able to compare that timestamp to the current time, and if it matches then I will fire the event. I even have another field that contains the current time and is automatically updated by INotifyPropertyChanged, but I can't seem to figure out a way to compare the timestamp of the new row to the field containing the current time.
I feel like there must be a solution to this, but after spending a frustrating day trying to figure it out I'm hoping someone out there will be able to shed some light.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在处理另一个问题时,我发现了一些可以帮助我解决这个问题的东西。方法1中的解决方案非常接近,它只是解决看似随机的其他行也在看似随机的时间突出显示的问题。问题是容器回收(此问题的更多信息:WPF Toolkit DataGrid Checkbox Issues) 。
无论如何,我所要做的就是将以下标记添加到我的数据网格中:
然后我使用与方法 1 中相同的触发器:
现在我可以在将新行插入到我的数据网格中时突出显示它们。非常有用!
While working on another issue I found something that helped me solve this problem. The solution in Method 1 is very close, it was just a matter of solving the problem of the seemingly random other rows also being highlighted at seemingly random times. The problem was container recycling (more information in this question: WPF Toolkit DataGrid Checkbox Issues).
Anyways, all I had to do was add the following tag to my datagrid:
And then I used the same trigger as in method 1:
Now I'm able to highlight new rows as they are inserted into my datagrid. Very useful!