Wpf:Storyboard.TargetName有效,但Setter TargetName没有

Ars*_*nko 6 wpf xaml storyboard

假设我们有一个像这样的XAML代码:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Border.LayoutTransform>
                        <!--We are rotating randomly each image. Selected one will be rotated to 45°.-->
                        <RotateTransform Angle="{Binding RandomAngle}" x:Name="globalRotation"/>
                    </Border.LayoutTransform>
                    <Grid>
                        <Image Source="{Binding ImageLocation}" Stretch="None" />
                        <TextBlock x:Name="title" Text="{Binding Title}" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="title" Property="Visibility" Value="Visible"/>
                        <!--The next line will not compile.-->
                        <Setter TargetName="globalRotation" Property="Angle" Value="45"/>
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <!--This compiles well.-->
                                    <DoubleAnimation Storyboard.TargetName="globalRotation" Storyboard.TargetProperty="Angle" To="45" Duration="00:00:03"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

此代码旨在在列表框中显示一组图像.每个图像都有一个随机旋转,但选中后,旋转到45度.

通过故事板旋转所选图像效果很好.我只是指定Storyboard.TargetName并在选择时旋转图像(Trigger.ExitActions省略以使代码更短).

现在,如果我想,而不是使用故事板,直接分配45度值,我不能这样做,因为<Setter TargetName="globalRotation" Property="Angle" Value="45"/>:它编译与

"无法找到触发器目标'globalRotation'.(目标必须出现在使用它的任何Setter,Triggers或Conditions之前.)"

错误.怎么了?我想这Storyboard.TargetName是在运行时评估的,所以让我编译它.这样对吗?

如何在不使用故事板的情况下使用setter工作?

小智 10

问题是Trigger Setter只能定位FrameworkElement或FrameworkContentElement对象,而Storyboard适用于任何DependencyProperty.

这是一个解决方法:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="brd" HorizontalAlignment="Center" VerticalAlignment="Center" Tag="{Binding RandomAngle}">                            
                    <Border.LayoutTransform>
                        <!--We are rotating randomly each image. Selected one will be rotated to 45°.-->
                        <RotateTransform Angle="{Binding ElementName=brd, Path=Tag}" x:Name="globalRotation"/>
                    </Border.LayoutTransform>
                    <Grid>
                        <Image Source="{Binding ImageLocation}" Stretch="None" />
                        <TextBlock x:Name="title" Text="{Binding Title}" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="title" Property="Visibility" Value="Visible"/>
                        <!--The next line will not compile.-->
                        <Setter TargetName="brd" Property="Tag" Value="45"/>
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <!--This compiles well.-->
                                    <DoubleAnimation Storyboard.TargetName="globalRotation" Storyboard.TargetProperty="Angle" To="45" Duration="00:00:03"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)