How to datatrigger an animation upon updated ViewModel property?

一曲冷凌霜 提交于 2019-12-03 16:43:18

As mentioned in the comment you can utilize Binding.TargetUpdated event.

Occurs when a value is transferred from the binding source to the binding target, but only for bindings with the NotifyOnTargetUpdated value set to true.

Which means that if value is pulled from view model into view, and NotifyOnTargetUpdated == True against binding, TargetUpdated event is raised. So it will be raised when value is initially displayed or later when you raise INotifyPropertyChanged.PropertyChanged event in your view model.

<DataGridTextColumn Header="Last Trade Price" Binding="{Binding Path=LastTradePrice, NotifyOnTargetUpdated=True}">
   <DataGridTextColumn.CellStyle>
      <Style TargetType="DataGridCell">
         <Style.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
               <BeginStoryboard>
                  <Storyboard>
                     <ColorAnimation To="Aqua" Duration="0:0:0.3" AutoReverse="True" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" />
                  </Storyboard>
               </BeginStoryboard>
            </EventTrigger>
         </Style.Triggers>
      </Style>
   </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Also if want to briefly notify with colour change you want to set AutoReverse="True" against ColorAnimation otherwise Aqua colour will stay. The only downside of this solution is that it will trigger also when DataGrid is created and initial values are loaded.

There is also Binding.SourceUpdated event which works with NotifyOnSourceUpdated against binding and works in the opposite direction to TargetUpdated event. It will be triggered when new value is transmitted from view to view model.

Gets or sets a value that indicates whether to raise the SourceUpdated event when a value is transferred from the binding target to the binding source.

By default both NotifyOnTargetUpdated abd NotifyOnSourceUpdated will be set to false to save on raising 2 additional event when values are transmitted between view and view model.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!