在 DataContext 更改时删除/停止情节提要

发布于 2024-08-24 04:38:29 字数 1542 浏览 4 评论 0原文

一般上下文: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2024-08-31 04:38:29

我会考虑向对象的​​ DataContextChanged 事件添加触发器。类似于:

<Style.Triggers>
    <EventTrigger EventName="DataContextChanged">
        <StopStoryboard Storyboard="{StaticResource OneSecondOpacityFlash}" />
    </EventTrigger>
</Style.Triggers>

不过,我想知道您是否想更改现有视图对象上的 DataContext,或者创建一个绑定到新 DataContext 的新视图是否会更好。根据您正在做的事情,我认为交换 DataContext 可能会导致保留额外的句柄。根据您的容器是什么,删除并重新创建子视图/视图模型可能比交换更容易。

I would consider adding a trigger to the DataContextChanged event on the object. Something like:

<Style.Triggers>
    <EventTrigger EventName="DataContextChanged">
        <StopStoryboard Storyboard="{StaticResource OneSecondOpacityFlash}" />
    </EventTrigger>
</Style.Triggers>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文