Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

datagrid - wpf - fire datatrigger when property changes regardless of new value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...