wpf - 无论新值如何,当属性发生变化时都会触发数据触发器 [英] wpf - fire datatrigger when property changes regardless of new value

查看:13
本文介绍了wpf - 无论新值如何,当属性发生变化时都会触发数据触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据网格单元格的值发生变化时,我试图在数据网格中的单元格上执行动画.

I'm trying to execute an animation on a cell in a datagrid when the value of the datagrid cell changes.

数据网格本身绑定到一个普通的旧 CLR 对象的 ObservableCollection.在这种情况下,假设对象是具有名字"、姓氏"和年龄"属性的人"对象.'Person' 类实现了 INotifyPropertyChanged 接口,并且每个属性在它的 setter 中都有对 onPropertyChanged 的​​适当调用.

The datagrid itself is bound to an ObservableCollection of plain old CLR objects. In this case lets say the objects are 'Person' objects with properties for 'Firstname', 'Lastname' and 'Age'. The 'Person' class implements the INotifyPropertyChanged interface and each property has the appropriate call to onPropertyChanged in it's setter.

这一切都很好.在数据网格定义中,我设置了用于绘制每个单元格的 DataTemplate 并附加了一个数据触发器......如下:

This is all fine. In the datagrid definition I've set my DataTemplate for drawing each cell and attached a datatrigger too ... as follows:

<DataGridTemplateColumn Header="FirstName">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Border Name="templateBorder">
                <TextBlock Name="templateTextBlock" Text="{Binding Path=FirstName}" />
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=FirstName}" Value="Richard">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard AutoReverse="True">
                                <DoubleAnimation Storyboard.TargetName="templateTextBlock" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

当我的 ObservableCollection 中的对象更新(我更改了 FirstName 值)时,数据网格会更新得很好.根据上面的示例,如果我将 FirstName 的值更改为 'Richard',那么动画也可以正常执行.

When an object in my ObservableCollection is updated (I changed the FirstName value) the datagrid is updated fine. As per the example above, if I changed the value of FirstName to 'Richard' then the animation is executed fine too.

我的问题是,无论 Firstname 的新值是什么,我都需要运行我的动画.我已经爬过网络,但有些人似乎只能找到针对已知值触发触发器的示例,例如正如我在示例中演示的那样,当 FirstName 为Richard"时触发触发器.

My problem is that I need to run my animation regardless of what the new value of Firstname is. I've crawled the web but some far only seem to find examples of firing the trigger against a known value e.g. fire trigger when FirstName is 'Richard' as I've demonstrated in my example.

我的问题是,无论更新属性的值如何,如何触发数据触发器?所以基本上我如何在数据网格中给定行的 FirstName 属性更新时触发数据触发器.

My question is how do I fire the datatrigger regardless of the value of the updated property? So basically how do I fire the datatrigger whenever the FirstName property is updated for a given row in the datagrid.

非常感谢.

推荐答案

感谢从对这个问题的回答中获得的指示,我发现答案是使用 EventTrigger 和 TargetUpdated RoutedEvent.

Thanks to the pointers gained from the responses to this question I found the answer was to use an EventTrigger and the TargetUpdated RoutedEvent.

<DataTemplate>
    <Border Name="templateBorder">
        <TextBlock Name="templateTextBlock" Text="{Binding Path=FirstName, NotifyOnTargetUpdated=True}" />
    </Border>
    <DataTemplate.Triggers>
        <EventTrigger RoutedEvent="Binding.TargetUpdated">
            <BeginStoryboard>
                <Storyboard AutoReverse="True">
                    <DoubleAnimation Storyboard.TargetName="templateTextBlock" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

除了 EventTrigger 之外,唯一需要做的就是在设置文本块绑定时设置NotifyOnTargetUpdated=True".

Beyond the EventTrigger, the only other thing that was required was to set 'NotifyOnTargetUpdated=True' when setting up the binding for the textblock.

谢谢.

这篇关于wpf - 无论新值如何,当属性发生变化时都会触发数据触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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