WPF DataGrid-插入到datagrid中时突出显示新行 [英] WPF DataGrid - highlight new rows when inserted into datagrid

查看:91
本文介绍了WPF DataGrid-插入到datagrid中时突出显示新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到ObservableCollection的数据网格,我想做的是在将新行添加到数据网格时(即,将新对象插入ObservableCollection时)突出显示新行.我想在插入行时突出显示这些行,方法是先更改背景颜色,然后随着时间的流逝使颜色逐渐恢复为正常.我尝试了各种不同的方法来使其正常工作,但没有任何工作可以正常工作.

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.

方法1:我有一个事件触发器,该事件触发器在列加载时触发.当元素加载时,它确实会触发,但似乎也几乎在其他旧行上触发(当行是新行时,它已经在其上触发过的行).

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>

方法2:当新项目添加到ObservableCollection时,我在视图模型中设置了布尔值.然后,我在触发器中检查此值,如果为true,则触发情节提要.但是,我无法使其正常运行,并且在运行该应用程序时,它会不断出错.另外,一旦情节提要板运行,我就无法找到一种将该值设置为false的方法(由于DataTrigger具有样式,因此我无法使用情节提要板的Completed事件).

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>

方法3:当新项目添加到可观察集合时,我尝试在视图模型中设置时间戳字段.然后,在XAML中,我希望能够将该时间戳与当前时间进行比较,如果匹配,则将触发该事件.我什至还有一个包含当前时间的字段,该字段由INotifyPropertyChanged自动更新,但是我似乎无法找出一种将新行的时间戳与包含当前时间的字段进行比较的方法.

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.

推荐答案

在处理另一个问题时,我发现了一些有助于解决该问题的方法.方法1中的解决方案非常接近,这只是解决看似随机的其他行也在看似随机的时间突出显示的问题.问题是容器回收(此问题中的更多信息: WPF工具包DataGrid复选框问题)

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:

VirtualizingStackPanel.VirtualizationMode="Standard"

然后我使用了与方法1相同的触发器:

And then I used the same trigger as in method 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>

现在,当新行插入到我的数据网格中时,我可以突出显示它们.非常有用!

Now I'm able to highlight new rows as they are inserted into my datagrid. Very useful!

这篇关于WPF DataGrid-插入到datagrid中时突出显示新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆