.net - 如何在更新 ViewModel 属性时触发动画?

标签 .net wpf mvvm datatrigger

我有以下 DataGrid 单元格,我想在底层 LastTradePrice 后立即为其背景颜色设置动画。属性改变它的值。

<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) 线路<DataTrigger Binding="{Binding LastTradePrice}" Value="True">虽然没有任何意义。

楼盘LastTradePrice显然不是要用 value = True 测量的 bool 值.每当更新属性时,如何使触发器触发?显然我已经实现了 INotification:
        public double LastTradePrice
        {
            get { return _model.LastTradePrice; }
            set
            {
                if (value != _model.LastTradePrice)
                {
                    LastTradePrice = value;
                    OnPropertyChanged("LastTradePrice");
                }
            }
        }

2) 如果我将整个样式定义存储在 <Window.Resources> 中,我将如何访问 ViewModels 属性 LastTradePrice ?

非常感谢

最佳答案

如评论中所述,您可以使用 Binding.TargetUpdated 事件。

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.



这意味着如果将值从 View 模型拉到 View 中,并且 NotifyOnTargetUpdated == True反对绑定(bind),TargetUpdated引发事件。因此,它会在最初显示值时引发,或者稍后在引发 INotifyPropertyChanged.PropertyChanged 时引发。 View 模型中的事件。
<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>

另外,如果想通过颜色更改简要通知您要设置 AutoReverse="True"反对ColorAnimation否则 Aqua颜色会留下来。此解决方案的唯一缺点是 它也会在 DataGrid 时触发创建并加载初始值 .

还有Binding.SourceUpdatedNotifyOnSourceUpdated 一起使用的事件反对绑定(bind),与 TargetUpdated 的工作方向相反事件。当新值从 View 传输到 View 模型时,它将被触发。

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当值在 View 和 View 模型之间传输时,将设置为 false 以节省引发 2 个附加事件。

关于.net - 如何在更新 ViewModel 属性时触发动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970985/

相关文章:

java - 从 Windows .NET 到 Android Java 的 Unicode 字符串

C# .NET AS2 通信

c# - 如何使用套接字将简单消息从 WinRT 客户端(.NET 4.5)发送到桌面服务器(.NET 4.0)?

c# - 如何还原最小/隐藏窗口?

c# - 如果尚未注册,请在 Prism 7.1 模块中注册单例

c# - WPF 的免费字体和颜色选择器?

c# - WPF:绑定(bind)选项卡控件

c# - WPF 最佳实践 : Do custom controls work well with the MVVM design?

wpf - 如何使工具栏按钮可展开?

c# - 处理 ViewModel/Model 中的致命异常