WPF:如何通过 bool 类型的 DependencyProperty DataTrigger 路径动画?
WPF: How can I DataTrigger a Path Animation by a DependencyProperty of type bool?
在过去的一周里,我试图找到一种触发路径动画的方法,但没有成功。
我想做的是使用我的 ViewModel 中定义的布尔值 属性,这样当该值为真时,矩形将沿着路径移动。
起初我认为这很容易,但是...
我发现的 Path-Animation 演示会通过 RoutedEvent 触发故事板,例如单击按钮或 Button.Loaded 等,但我没有办法通过 DependencyProperty 触发它。
我是 WPF 的新手,在此先感谢您!
代码在这里:
<!--I define a rectangle which is expected to be auto-moving along the path when "Monitoring" is set true. -->
<Rectangle Width="20" Height="10" Fill="LightBlue">
<Rectangle.RenderTransform>
<MatrixTransform x:Name="RectangleMatrixTransform">
<MatrixTransform.Matrix >
<Matrix />
</MatrixTransform.Matrix>
</MatrixTransform>
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Monitoring}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<!--Here I got compile exception: 'TargetName property cannot be set on a Style Setter.'-->
<MatrixAnimationUsingPath
Storyboard.TargetName="RectangleMatrixTransform"
Storyboard.TargetProperty="Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
RepeatBehavior="Forever" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"
PresentationOptions:Freeze="True" />
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Canvas>
顺便说一句,WPF 很强大但真的很难:(
只需删除 Storyboard.TargetName
并使用 Storyboard.TargetProperty="RenderTransform.Matrix"
:
<Rectangle Width="20" Height="10" Fill="LightBlue">
<Rectangle.RenderTransform>
<MatrixTransform />
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Monitoring}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath
Storyboard.TargetProperty="RenderTransform.Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
RepeatBehavior="Forever" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
在过去的一周里,我试图找到一种触发路径动画的方法,但没有成功。
我想做的是使用我的 ViewModel 中定义的布尔值 属性,这样当该值为真时,矩形将沿着路径移动。
起初我认为这很容易,但是...
我发现的 Path-Animation 演示会通过 RoutedEvent 触发故事板,例如单击按钮或 Button.Loaded 等,但我没有办法通过 DependencyProperty 触发它。
我是 WPF 的新手,在此先感谢您!
代码在这里:
<!--I define a rectangle which is expected to be auto-moving along the path when "Monitoring" is set true. -->
<Rectangle Width="20" Height="10" Fill="LightBlue">
<Rectangle.RenderTransform>
<MatrixTransform x:Name="RectangleMatrixTransform">
<MatrixTransform.Matrix >
<Matrix />
</MatrixTransform.Matrix>
</MatrixTransform>
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Monitoring}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<!--Here I got compile exception: 'TargetName property cannot be set on a Style Setter.'-->
<MatrixAnimationUsingPath
Storyboard.TargetName="RectangleMatrixTransform"
Storyboard.TargetProperty="Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
RepeatBehavior="Forever" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100"
PresentationOptions:Freeze="True" />
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Canvas>
顺便说一句,WPF 很强大但真的很难:(
只需删除 Storyboard.TargetName
并使用 Storyboard.TargetProperty="RenderTransform.Matrix"
:
<Rectangle Width="20" Height="10" Fill="LightBlue">
<Rectangle.RenderTransform>
<MatrixTransform />
</Rectangle.RenderTransform>
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<DataTrigger Binding="{Binding Monitoring}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<MatrixAnimationUsingPath
Storyboard.TargetProperty="RenderTransform.Matrix"
DoesRotateWithTangent="True"
Duration="0:0:5"
RepeatBehavior="Forever" >
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry Figures="M 10,100 C 35,0 135,0 160,100 180,190 285,200 310,100" />
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>