动画旋转 PathGeometry
Animation Rotating a PathGeometry
我的路径具有三个定义的 PathGeometries:圆、连接线和代表风扇叶片的路径。我想使用路径的标签 属性 来触发旋转风扇叶片几何体的动画。由于我需要多次重复使用它,如果可能的话,我还想将路径和故事板包含在单一样式中。
到目前为止,我已经建立了路径,创建了故事板,在我想要旋转的 PathGeometry 上创建了旋转变换,并创建了必要的触发器。
我不明白为什么以下方法不起作用:
<Style x:Key="fanPath" TargetType="{x:Type Path}">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry>
<PathFigure StartPoint="15,30" IsFilled="False">
<LineSegment Point="15,50"/>
</PathFigure>
</PathGeometry>
<EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
<!-- Want to rotate the following -->
<PathGeometry>
<PathGeometry.Transform>
<RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
</PathGeometry.Transform>
<PathFigure StartPoint="10,5" IsClosed="True">
<LineSegment Point="20,5"/>
<LineSegment Point="10,25"/>
<LineSegment Point="20,25"/>
</PathFigure>
<PathFigure StartPoint="5,10" IsClosed="True">
<LineSegment Point="5,20"/>
<LineSegment Point="25,10"/>
<LineSegment Point="25,20"/>
</PathFigure>
</PathGeometry>
</GeometryGroup>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="fanRotate">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="rotate.Angle" From="0"
To="90" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="fanRotate"/>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
我检查过我的标签 属性 设置是否正确,我检查过手动更改旋转变换的角度 属性 是否按预期工作。我相信我的问题在于将 Storyboard.TargetProperty 属性 链接到正确的位置 (rotate.Angle),但我无法弄清楚我遇到的核心问题。
您需要通过如下路径访问您的 Angle
属性:
Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle"
这就是您所需要的:
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="fanPath" TargetType="{x:Type Path}">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry>
<PathFigure StartPoint="15,30" IsFilled="False">
<LineSegment Point="15,50"/>
</PathFigure>
</PathGeometry>
<EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
<!-- Want to rotate the following -->
<PathGeometry>
<PathGeometry.Transform>
<RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
</PathGeometry.Transform>
<PathFigure StartPoint="10,5" IsClosed="True">
<LineSegment Point="20,5"/>
<LineSegment Point="10,25"/>
<LineSegment Point="20,25"/>
</PathFigure>
<PathFigure StartPoint="5,10" IsClosed="True">
<LineSegment Point="5,20"/>
<LineSegment Point="25,10"/>
<LineSegment Point="25,20"/>
</PathFigure>
</PathGeometry>
</GeometryGroup>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="fanRotate">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle" From="0"
To="90" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="fanRotate"/>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Path x:Name="paththing" Width="300" Height="300" Fill="Aqua" Stretch="Fill" Style="{StaticResource fanPath}"/>
<Button Grid.Row="1" x:Name="button" Content="Go" VerticalAlignment="Bottom">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="paththing" Storyboard.TargetProperty="Tag">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="True"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Window>
我的路径具有三个定义的 PathGeometries:圆、连接线和代表风扇叶片的路径。我想使用路径的标签 属性 来触发旋转风扇叶片几何体的动画。由于我需要多次重复使用它,如果可能的话,我还想将路径和故事板包含在单一样式中。
到目前为止,我已经建立了路径,创建了故事板,在我想要旋转的 PathGeometry 上创建了旋转变换,并创建了必要的触发器。
我不明白为什么以下方法不起作用:
<Style x:Key="fanPath" TargetType="{x:Type Path}">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry>
<PathFigure StartPoint="15,30" IsFilled="False">
<LineSegment Point="15,50"/>
</PathFigure>
</PathGeometry>
<EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
<!-- Want to rotate the following -->
<PathGeometry>
<PathGeometry.Transform>
<RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
</PathGeometry.Transform>
<PathFigure StartPoint="10,5" IsClosed="True">
<LineSegment Point="20,5"/>
<LineSegment Point="10,25"/>
<LineSegment Point="20,25"/>
</PathFigure>
<PathFigure StartPoint="5,10" IsClosed="True">
<LineSegment Point="5,20"/>
<LineSegment Point="25,10"/>
<LineSegment Point="25,20"/>
</PathFigure>
</PathGeometry>
</GeometryGroup>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="fanRotate">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="rotate.Angle" From="0"
To="90" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="fanRotate"/>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
我检查过我的标签 属性 设置是否正确,我检查过手动更改旋转变换的角度 属性 是否按预期工作。我相信我的问题在于将 Storyboard.TargetProperty 属性 链接到正确的位置 (rotate.Angle),但我无法弄清楚我遇到的核心问题。
您需要通过如下路径访问您的 Angle
属性:
Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle"
这就是您所需要的:
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="fanPath" TargetType="{x:Type Path}">
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
<Setter Property="Data">
<Setter.Value>
<GeometryGroup>
<PathGeometry>
<PathFigure StartPoint="15,30" IsFilled="False">
<LineSegment Point="15,50"/>
</PathFigure>
</PathGeometry>
<EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
<!-- Want to rotate the following -->
<PathGeometry>
<PathGeometry.Transform>
<RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
</PathGeometry.Transform>
<PathFigure StartPoint="10,5" IsClosed="True">
<LineSegment Point="20,5"/>
<LineSegment Point="10,25"/>
<LineSegment Point="20,25"/>
</PathFigure>
<PathFigure StartPoint="5,10" IsClosed="True">
<LineSegment Point="5,20"/>
<LineSegment Point="25,10"/>
<LineSegment Point="25,20"/>
</PathFigure>
</PathGeometry>
</GeometryGroup>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Tag" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="fanRotate">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle" From="0"
To="90" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="fanRotate"/>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Path x:Name="paththing" Width="300" Height="300" Fill="Aqua" Stretch="Fill" Style="{StaticResource fanPath}"/>
<Button Grid.Row="1" x:Name="button" Content="Go" VerticalAlignment="Bottom">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="paththing" Storyboard.TargetProperty="Tag">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="True"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</Window>