我有一个地铁应用程序,它有一个包含一行矩形对象的网格,我想同时为所有矩形设置动画(在 1 秒内从 0 缩放到 1)。我想尽可能多地使用资源,避免代码重复。网格的基本骨架如下。
<Grid>
<Grid.Resources>
<Style TargetType="Rectangle" x:Key="RectangleStyle">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Margin" Value="10"/>
<Setter Property="RadiusX" Value="10"/>
<Setter Property="RadiusY" Value="10"/>
<Setter Property="RadiusY" Value="10"/>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform x:Name="RectangleScaleTransform" ScaleX="1" ScaleY="1" />
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="RectangleScaleTransform"
Storyboard.TargetProperty="(ScaleTransform.ScaleX)" From="0" To="1.0" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="RectangleScaleTransform"
Storyboard.TargetProperty="(ScaleTransform.ScaleY)" From="0" To="1.0" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="0" Style="{StaticResource RectangleStyle}" />
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="1" Style="{StaticResource RectangleStyle}" />
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="2" Style="{StaticResource RectangleStyle}"/>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="3" Style="{StaticResource RectangleStyle}"/>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="4" Style="{StaticResource RectangleStyle}"/>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="5" Style="{StaticResource RectangleStyle}"/>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="6" Style="{StaticResource RectangleStyle}"/>
<Rectangle Fill="Red" Grid.Row="0" Grid.Column="7" Style="{StaticResource RectangleStyle}"/>
</Grid>
</Grid>
我不想向RenderTransform
每个单独的矩形添加触发器,也不想向每个矩形三角形添加触发器,因为这似乎有很多重复。使用上面的代码,我得到了一个Cannot resolve TargetName "RectangleScaleTransform
错误。
任何帮助都会很棒。