uwp 动态情节提要目标名称

uwp dynamic storyboard target name

我在 ListView.ItemTemplate 中有一个 Button,它模仿下拉按钮的行为。我想使用 StoryBoard.

为按钮提供旋转动画
<Storyboard x:Name="FolderPathItemArrowAnimation">
    <DoubleAnimation
        By="90"
        Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
        Duration="0:0:0.25" />
</Storyboard>

<ListView
     Background="AntiqueWhite"
     HorizontalAlignment="Stretch"
     ItemsSource="{x:Bind FolderChain, Mode=OneWay}"
     ItemContainerStyle="{StaticResource StretchListViewItemStyle}"
     Style="{StaticResource HorizontalListViewStyle}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="models:FolderTree">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Button
                            Style="{StaticResource FolderChainButtonStyle}"
                            Click="PathItemButton_Click" >
                            <Button.Content>
                                <TextBlock Text="{x:Bind Name}" />
                            </Button.Content>
                        </Button>
                        <Button
                            Name="{x:Bind Id, Converter={StaticResource FolderChainDropdownButtonNameConverter}}"
                            Grid.Column="1"
                            Width="30"
                            Click="PathItemDropDownButton_Click"
                            Style="{StaticResource FolderChainButtonStyle}"
                            Content="&#xE974;"
                            FontFamily="Segoe MDL2 Assets"
                            RenderTransformOrigin=".5,.5" >
                            <Button.RenderTransform>
                                <RotateTransform />
                            </Button.RenderTransform>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

下面是启动动画的代码。当 运行 时,我得到一个异常说 Cannot resolve TargetName FolderChainItemDropdownButton1。如何在 ListView.ItemTemplate 上使用 StoryBoard

    private void PathItemDropDownButton_Click(object sender, RoutedEventArgs e)
    {
        Storyboard.SetTargetName(FolderPathItemArrowAnimation, (sender as FrameworkElement).Name);
        FolderPathItemArrowAnimation.Begin();
    }

get an Exception says that Cannot resolve TargetName FolderChainItemDropdownButton1

看起来 SetTargetName 方法无法在 DataTemplate 下获取正确的元素。请尝试用SetTarget的方法替换

Storyboard.SetTarget(FolderPathItemArrowAnimation, (sender as FrameworkElement));