如何datatrigger在更新视图模型属性的动画? [英] How to datatrigger an animation upon updated ViewModel property?

查看:119
本文介绍了如何datatrigger在更新视图模型属性的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的DataGrid单元格,这是我想为标的的 LastTradePrice 属性来更改它的值简要尽快动画的背景色。

I have the following DataGrid cell, which I would like to animate its background colour briefly as soon as the underlying LastTradePrice property changes its value.

<DataGridTextColumn Header="Last Trade Price" Binding="{Binding LastTradePrice}">
     <DataGridTextColumn.CellStyle>
          <Style TargetType="DataGridCell">
             <Style.Triggers>
                // ???
                <DataTrigger Binding="{Binding LastTradePrice}" Value="True"> 
                     <DataTrigger.EnterActions>
                          <BeginStoryboard>
                              <Storyboard>
                                 <ColorAnimation To="Aqua" Duration="0:0:0.3" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"/>
                              </Storyboard>
                          </BeginStoryboard>
                      </DataTrigger.EnterActions>
                 </DataTrigger>
             </Style.Triggers>
          </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

1)该行&LT; D​​ataTrigger绑定={结合LastTradePrice}VALUE =真&GT; 是没有意义的,虽然

1) The line <DataTrigger Binding="{Binding LastTradePrice}" Value="True"> makes no sense though.

属性 LastTradePrice 显然不是一个布尔值,与值= TRUE 测量。我怎样才能让触发器触发每当属性更新?很显然,我有INotification实现:

The property LastTradePrice is obviously not a boolean to be measured with value = True. How can I make the trigger to fire whenever the property is updated? Obviously I have INotification implemented:

        public double LastTradePrice
        {
            get { return _model.LastTradePrice; }
            set
            {
                if (value != _model.LastTradePrice)
                {
                    LastTradePrice = value;
                    OnPropertyChanged("LastTradePrice");
                }
            }
        }

2)。如果我会保存在里面&LT整体风格定义; Window.Resources&GT; ,我将如何访问过的ViewModels属性 LastTradePrice

2) If I would have stored the whole style definition inside <Window.Resources>, how would I have accessed the ViewModels property LastTradePrice?

非常感谢

推荐答案

正如你可以利用<一个评论href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.targetupdated%28v=vs.110%29.aspx"><$c$c>Binding.TargetUpdated事件。

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

当值从绑定源传输到绑定目标,但仅限于与绑定设置为true NotifyOnTargetUpdated值出现。

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.

这意味着,如果值是从视图模型拖入视图, NotifyOnTargetUpdated ==真对结合, TargetUpdated 事件引发。所以这将是在最初显示的值或在您的视图模型提出后,当你举起 INotifyPropertyChanged.PropertyChanged 事件。

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>

此外,如果想要的简要通知的要设置颜色的变化自动翻转=真 Col​​orAnimation ,否则水族颜色会留下来。这种解决方案的唯一缺点是,它也将引发当的DataGrid 创建和初始值装入

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.

还有 Binding.SourceUpdated 事件,适用于 NotifyOnSourceUpdated 针对绑定和工作在相反的方向 TargetUpdated 事件。当新的值从图发送,以查看模型将被触发。

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.

获取或设置一个值,该值指示是否提高SourceUpdated事件当值从绑定目标传输到绑定源。

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.

在默认情况下这两个 NotifyOnTargetUpdated ABD NotifyOnSourceUpdated 将被设置为false,以节省提高2个额外的事件,当值之间的观点和看法模式传送。

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.

这篇关于如何datatrigger在更新视图模型属性的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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