在 DataContext 更改时删除/停止情节提要
一般上下文:MVVM 应用程序。
我有一个名为 JobView 的视图。它的DataContext是一个名为Job的类。 Job 中有一个名为 AuthorizationNeeded 的属性。
视图中的边框具有样式(来自资源字典),并且该样式具有数据触发器,该触发器根据绑定属性 AuthorizationNeeded 启动和停止情节提要。
<Style x:Key="AuthorizationNeededBorder"
TargetType="Border">
<Setter Property="Background"
Value="Yellow" />
<Setter Property="Opacity"
Value="0" />
<Style.Triggers>
<DataTrigger Binding="{Binding AuthorizationNeeded, FallbackValue=False}"
Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="Flash"
Storyboard="{StaticResource OneSecondOpacityFlash}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="Flash" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
一切都按预期进行。更改 AuthorizationNeeded 的值会在更改为 true 时启动故事板闪烁,并在更改为 false 时删除(并停止)故事板。
但是,如果我在 Storyboard 运行时将 JobView 的 DataContext 更改为不同的 ViewModel(不同的 Job),即使新 Job 中的 AuthorizationNeeded 值为 false,storyboard 也会继续运行。
数据触发器没有看到 AuthorizationNeeded true -> 值的变化DataContext 更改期间为 false。
关于如何实现 AuthorizationNeed = true = storboard 运行到 AuthorizationNeeded = false = Storyboard 不在所有情况下运行的所需行为的任何想法将不胜感激。 (我不想在 DataContext 更改时手动更改 AuthorizationNeeded 的值,因为实际上此视图上有很多此类触发器......)
General context : MVVM application.
I have a View called JobView. Its DataContext is a class called Job. Within Job is a property called AuthorizationNeeded.
A Border in the view has a style (from a resource dictionary) and that style has a data trigger which starts and stops a storyboard based on the bound property AuthorizationNeeded.
<Style x:Key="AuthorizationNeededBorder"
TargetType="Border">
<Setter Property="Background"
Value="Yellow" />
<Setter Property="Opacity"
Value="0" />
<Style.Triggers>
<DataTrigger Binding="{Binding AuthorizationNeeded, FallbackValue=False}"
Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="Flash"
Storyboard="{StaticResource OneSecondOpacityFlash}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="Flash" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
Everything works as expected. Changing AuthorizationNeeded's value starts the storyboard flash when moving to true and removes (and stops) the storyboard when moving to false.
However, if I change the DataContext of JobView to a different ViewModel (a different Job) while the storyboard is running, even if the value of AuthorizationNeeded is false in the new Job, the storyboard continues to run.
The data trigger is not seeing the change of value from AuthorizationNeeded true -> false during the DataContext change.
Any ideas on how I can get to the desired behavior of AuthorizationNeed = true = storboard running to AuthorizationNeeded = false = storyboard not running under all circumstances would be greatly appreciated. (I would prefer not to manually change the value of AuthorizationNeeded at a DataContext change because in reality there are many such triggers on this view...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会考虑向对象的
DataContextChanged
事件添加触发器。类似于:不过,我想知道您是否想更改现有视图对象上的 DataContext,或者创建一个绑定到新 DataContext 的新视图是否会更好。根据您正在做的事情,我认为交换 DataContext 可能会导致保留额外的句柄。根据您的容器是什么,删除并重新创建子视图/视图模型可能比交换更容易。
I would consider adding a trigger to the
DataContextChanged
event on the object. Something like:I would wonder, though, if you want to change the DataContext on an existing view object or if it would be better to create a new view bound to the new DataContext. Depending on what you're doing, I would think that swapping out DataContexts could result in extra handles being held. Depending on what your container is, it may be easier to remove and re-create the child view/viewmodel than to swap.