gpt4 book ai didi

wpf - MVVM 中的 Storyboard动画

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

我正在尝试淡入文本 block 然后淡出以在 MVVM 中显示成功消息,但我无法让它再次淡出。我看了这个:WPF MVVM Property Change Animation但不要真的跟随。
这里是风格:

 <Style TargetType="TextBlock" x:Key="fadeinout">
<Style.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)" From="0.0" To="1.0" Duration="0:0:5"/>
<DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)" From="1.0" To="0.0" Duration="0:0:5"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>

并且在 View 中
<TextBlock Text="{Binding Path=SuccessMessage}" Style="{StaticResource fadeinout}"  Width="60"/>

在 View 模型中,保存后我这样做:
SuccessMessage = "Success";

其中 SuccessMessage 是具有调用 PropertyChanged 的​​ setter 的属性。我想我希望将文本固定为“您的保存成功”之类的内容,但无论如何我希望 View 模型能够做一些事情,使文本 block 淡入然后再次淡出,并在用户再次保存时重复。我需要做什么?

编辑 (我不能自我回答 8 小时):
必须将 NotifyOnTargetUpdated 添加到绑定(bind)中,现在它淡入淡出:
 <TextBlock Text="{Binding SuccessMessage, NotifyOnTargetUpdated=True}" Style="{StaticResource fadeInOut}"

根据 Jakob 的回答,我现在有了一个 TextBox(但将 EventTrigger 更改为 Binding.TargetUpdated),一个像上面这样的 TextBlock 和一个像这样的 TextBlock 内联:
<TextBlock Text="{Binding SuccessMessage, NotifyOnTargetUpdated=True}" Opacity="0" x:Name="tbMessage" Width="60" HorizontalAlignment="Left">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="tbMessage" Storyboard.TargetProperty="Opacity" To="1" BeginTime="0:0:0" Duration="0:0:1" />
<DoubleAnimation Storyboard.TargetName="tbMessage" Storyboard.TargetProperty="Opacity" To="0" BeginTime="0:0:3" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>

每次保存时,所有三个都会淡入淡出。奇怪的是,尽管使用了相同的时间,但模板化的一个稍微落后于其他两个。

最佳答案

尝试设置 DoubleAnimation.BeginTime .这样你就可以推迟淡出动画的开始时间,这样它就不会在淡入完成之前开始。在以下示例中,文本在淡出之前显示 2 秒:

<DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)" From="0.0" To="1.0" Duration="0:0:5" BeginTime="0:0:0" />
<DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)" From="1.0" To="0.0" Duration="0:0:5" BeginTime="0:0:7" />

或者,您可以使用 AutoReverse 但是您将无法控制在淡出之前显示文本的时间。

更新:

使用 TextBlock 的事件触发器似乎存在一些问题,因为 TextBlock 没有任何可以在触发器中使用的适当事件。以下是我使用 TextBox 而不是 TextBlock 的工作示例。您始终可以设置 TextBox 的样式,使其看起来像 TextBlock,即使 TextBlock 更轻量级:
<TextBox Text="{Binding SuccessMessage}" Opacity="0" x:Name="textBoxMessage" >
<TextBox.Triggers>
<EventTrigger RoutedEvent="TextBoxBase.TextChanged">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="textBoxMessage"
Storyboard.TargetProperty="Opacity"
To="1"
BeginTime="0:0:0" Duration="0:0:1"
/>
<DoubleAnimation
Storyboard.TargetName="textBoxMessage"
Storyboard.TargetProperty="Opacity"
To="0"
BeginTime="0:0:3" Duration="0:0:1"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>

关于wpf - MVVM 中的 Storyboard动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297838/

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