WPF。如何通过绑定停止数据触发动画?
在 WPF 工具包数据网格中,我有一个绑定到单元格元素不透明度的数据触发器。
当 UpVisibility
更改为 1 时,路径变得可见,并且动画开始将其淡化为 0。这有效。
然而我现在的问题 - 如果我需要提前停止/取消褪色并将 UpVisibility
设置为 0,路径仍然可见并褪色,因为什么也没发生......
如何将不透明度降低到0 立即使用 MyValue 对象?
<Path Data="M 5,0 0,10 10,10" Height="10" Width="10" Fill="Green" Opacity="{Binding MyValue[0].UpVisibility}" Margin="5,0,5,0">
<Path.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding MyValue[0].UpVisibility}" Value="1.0">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
In WPF toolkit datagrid I have a data trigger bound to opacity of cell element.
When UpVisibility
changes to 1 the path become visible and the animation starts to fade it to 0. Which works.
However my problem now - if I need to prematurely stop/cancel the fading and am setting UpVisibility
to 0 the Path is still visible and fading as nothing happened....
How to drop opacity to 0 instantly using MyValue object ?
<Path Data="M 5,0 0,10 10,10" Height="10" Width="10" Fill="Green" Opacity="{Binding MyValue[0].UpVisibility}" Margin="5,0,5,0">
<Path.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding MyValue[0].UpVisibility}" Value="1.0">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:10" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
还可以使用
DataTrigger
上的ExitAction
来停止故事板,当绑定值更改为超出目标状态时调用。只需给您的BeginStoryboard
命名,并在StopStoryboard
操作中引用它,如下所示:这可能比启动第二个故事板来停止或屏蔽不同的故事板更合适。
Storyboards can also be stopped using the
ExitAction
on aDataTrigger
, which is called when the bound value changes out of the target state. Just give yourBeginStoryboard
a name, and reference it in aStopStoryboard
action, like so:This may be more appropriate than starting a second storyboard to stop or mask a different storyboard.
您是否尝试过类似的操作(未经测试的示例):
我将动画持续时间设置为 0 以使其即时。如果您没有定义 From 属性,您将指示 WPF 以当前 DependencyProperty 值启动,因此它将顺利过渡。
Have you tried something like this (untested example):
I set the animation duration to 0 to be instant. And if you don't define the From property you are instructing WPF to start in the current DependencyProperty value, so it will transition smoothly.
如果您需要在后面的代码中重置动画,您通常会执行这样的调用:
但是如何根据 MVVM 在 XAML 中执行此操作?答案是:
要在 ViewModel 中调用动画,我们可以这样做:
同时确保绑定属性(在我的例子中消失)调用 INotifyPropertyChanged 来通知视图新值
If you need to reset animation in code behind you usually do such call:
But how to do it in XAML according to MVVM? Answer is:
To call animation in ViewModel we can do:
Also make sure that binding property (Vanishing in my case) calls INotifyPropertyChanged to notify view about new value