wpf-fire datatrigger当属性更改,不管新值 [英] wpf - fire datatrigger when property changes regardless of new value

查看:156
本文介绍了wpf-fire datatrigger当属性更改,不管新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数据网格本身绑定到一个ObservableCollection的ObservableCollection简单的老CLR对象。在这种情况下,我们可以说对象是'Person'对象,其属性为'Firstname','Lastname'和'Age'。 'Person'类实现了INotifyPropertyChanged接口,并且每个属性在其setter中都调用了onPropertyChanged。



这一切都很好。在datagrid定义中,我设置了我的DataTemplate来绘制每个单元格并附加一个datatrigger,如下所示:

  ; DataGridTemplateColumn Header =FirstName> 
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Border Name =templateBorder>
< TextBlock Name =templateTextBlockText ={Binding Path = FirstName}/>
< / Border>
< DataTemplate.Triggers>
< DataTrigger Binding ={Binding Path = FirstName}Value =Richard>
< DataTrigger.EnterActions>
< BeginStoryboard>
< Storyboard AutoReverse =True>
< DoubleAnimation Storyboard.TargetName =templateTextBlockStoryboard.TargetProperty =OpacityTo =。1Duration =0:0:.5/>
< / Storyboard>
< / BeginStoryboard>
< /DataTrigger.EnterActions>
< / DataTrigger>
< /DataTemplate.Triggers>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>

当我的ObservableCollection中的对象被更新(我更改了FirstName值)时,datagrid更新了。根据上面的例子,如果我将FirstName的值更改为Richard,那么动画也执行得很好。



我的问题是我需要运行我的动画不管Firstname的新价值是什么。我已经爬网了,但是一些远远只是找到了针对已知值触发触发器的例子。 FirstName是Richard的触发器,如我在示例中所示。



我的问题是如何触发datatrigger,而不管更新的属性的值?所以基本上如何在datagrid的给定行更新FirstName属性时,如何触发datatrigger。



非常感谢。

解决方案

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

 < DataTemplate> 
< Border Name =templateBorder>
< TextBlock Name =templateTextBlockText ={Binding Path = FirstName,NotifyOnTargetUpdated = True}/>
< / Border>
< DataTemplate.Triggers>
< EventTrigger RoutedEvent =Binding.TargetUpdated>
< BeginStoryboard>
< Storyboard AutoReverse =True>
< DoubleAnimation Storyboard.TargetName =templateTextBlockStoryboard.TargetProperty =OpacityTo =。1Duration =0:0:.5/>
< / Storyboard>
< / BeginStoryboard>
< / EventTrigger>
< /DataTemplate.Triggers>
< / DataTemplate>

除了EventTrigger之外,唯一需要设置的是NotifyOnTargetUpdated = True加载文本框的绑定。



谢谢。


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

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.

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>

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.

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.

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.

Many thanks.

解决方案

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>

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

Thanks.

这篇关于wpf-fire datatrigger当属性更改,不管新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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