gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:22:02 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com