gpt4 book ai didi

c# - 数据更改的 WPF 动画

转载 作者:行者123 更新时间:2023-11-30 22:01:12 25 4
gpt4 key购买 nike

我正在处理列出一些对象的 WPF 网格。如果对象的数据发生变化,我想启动一个动画。

下面列出了 XAML 代码的摘录

    <ListView Name="ListViewEmployeeDetails" Grid.Row="1" Margin="4,109,12,23"  ItemsSource="{Binding Products}"  >
<ListView.View>
<GridView x:Name="grdTest">
<GridViewColumn Header="ID" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="idField" Text="{Binding ID}" TextDecorations="Underline" Foreground="Blue"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ID}">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:5" To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="idField"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="100" />
<GridViewColumn Header="Reliab" DisplayMemberBinding="{Binding Reliability}" Width="100" />
</GridView>
</ListView.View>
</ListView>

每当属性更改时,我都会触发 PropertyChangedEvent。例如,ID 的 setter 如下所示:

        set
{
m_ID = value;
OnPropertyChanged("ID");
}

OnPropertyChanged 函数的样子:

    #region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion

但是,当 ID 更改时,不会触发动画。知道如何解决这个问题吗?

最佳答案

您应该使用事件触发器:

尝试这样的事情:

<DataTemplate.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:5" To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="idField"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DataTemplate.Triggers>

编辑:不要忘记在您的模板中进行设置!

<TextField Name="idField" Text="{Binding ID, NotifyOnTargetUpdated=True}" />

关于c# - 数据更改的 WPF 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926451/

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