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

时间:2010-05-24 05:06:47

标签: 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>

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

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

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

编译

“无法找到触发器目标'globalRotation'。(目标必须出现在任何使用它的Setter,Triggers或条件之前。)”

错误。怎么了?我想在运行时期间会计算Storyboard.TargetName,所以让我编译它。是不是?

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

1 个答案:

答案 0 :(得分: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>