c# - 在 DataTrigger 中绑定(bind) Storyboard 动画会使 XamlParser 崩溃

标签 c# wpf mvvm storyboard datatrigger

我希望我的应用程序在每次发生特定事件时将椭圆动画到一个新位置。出于测试目的,我制作了一个更改 viewmodel 属性的按钮,并且此属性绑定(bind)到触发动画的数据触发器,但后来我希望 viewmodel 根据我尚未实现的其他事件触发它 - 这就是为什么我可以'直接在 View 中使用绑定(bind)到该按钮的事件触发器,我需要 View 模型。 因为椭圆应该在每次被触发时被动画移动到一个新位置,所以我需要将 DoubleAnimation 的 TO-Property 绑定(bind)到 View 模型中的一个值。这在我使用普通事件触发器时工作正常,但使用数据触发器会使具有该绑定(bind)的 XamlParser 崩溃。除了 XAMLParseException 之外,我没有收到任何特定错误,但是,如果我将绑定(bind)更改为固定值(例如 5),它将正常工作。

这是 xaml:

<Window x:Class="AnimationExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AnimationExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>

    <Canvas Name="CanvasWrapper" Width="525" Height="350">
        <Button Content="Next Animation" Command="{Binding NextAnimation}"/>
            <Ellipse Fill="Red" Width="60" Height="60" Canvas.Left="60" Canvas.Top="60">
            <Ellipse.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CanAnimate}" Value="True" >
                            <DataTrigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <!-- Works fine without the binding -->
                                        <DoubleAnimation
                                            Storyboard.TargetProperty="(Canvas.Left)"
                                            Duration="0:0:10" To="{Binding NextPosX}" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
        </Ellipse>
    </Canvas>
</Window>

我相信解析器无法找到绑定(bind)的正确路径,即使代码完成显示了它。有什么方法可以获得绑定(bind)的正确路径吗?

最佳答案

阅读 Data Binding and Animating Animations 部分:基本上动画是卡住创建的(即它的所有属性都是只读的,因此不能修改),必须重新创建动画以反射(reflect)属性更改通知。

看看这个blog entrythis特种部队问题。

根据您的评论更新:

首先为什么 DataTrigger 和 StoryBoard 绑定(bind)不起作用?

The answer is provided by Storyboards Overview as follows:

You can't use dynamic resource references or data binding expressions to set Storyboard or animation property values. That's because everything inside a Style must be thread-safe, and the timing system must Freeze Storyboard objects to make them thread-safe. A Storyboard cannot be frozen if it or its child timelines contain dynamic resource references or data binding expressions. For more information about freezing and other Freezable features, see the Freezable Objects Overview.

为什么绑定(bind)适用于 EventTrigger 但不适用于 DataTrigger?

再次来自同一来源:

Animations applied by property Trigger objects behave in a more complex fashion than EventTrigger animations or animations started using Storyboard methods. They "handoff" with animations defined by other Trigger objects, but compose with EventTrigger and method-triggered animations.

我将假设他们的意思是,对于 EventTriggers,StoryBoard 是在事件发生时重新创建的,但对于 DataTriggers,情况并非如此 - “切换”似乎意味着共享 Storyboard。我将创建一个 SO 问题来验证这是什么意思(此处创建的问题:Storyboard: EventTrigger vs DataTrigger)。

关于c# - 在 DataTrigger 中绑定(bind) Storyboard 动画会使 XamlParser 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253615/

相关文章:

c# - jQuery - 如何正确显示两个日期之间的所有记录?

c# - C# 中如何表示不安全指针

c# - 为什么异步客户端 TCP 操作的性能似乎比同步 TCP 操作差?

c# - 有没有办法知道来电者的程序名称?

c# - 为按钮设置背景和不透明度但不影响其内容

c# - 如何在 MVVM 中绑定(bind)选定项目

java - ZK MVVM 中的表单验证(服务器端)示例

c# - StackPanel 高度超过父 Grid 高度

c# - 是否有 IValueConverter 执行 if-then

c# - MVVM Light - RaiseCanExecuteChanged 用于 RelayCommand