2010-05-24 16 views
6

Digamos que tenemos un código XAML como esto:Wpf: Storyboard.TargetName funciona, pero no se Setter TargetName

<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> 

Este código está destinado a mostrar una serie de imágenes en un cuadro de lista. Cada imagen tiene una rotación aleatoria, pero cuando se selecciona, gira a 45 grados.

Rotar la imagen seleccionada a través de un guión gráfico funciona bien. Solo especifico Storyboard.TargetName y gira la imagen cuando se selecciona (Trigger.ExitActions se omite para acortar el código).

Ahora, si quiero, en lugar de utilizar un guión gráfico, asignar un valor de 45 grados directamente, no puedo hacer eso, porque <Setter TargetName="globalRotation" Property="Angle" Value="45"/>: compila con

"No se puede encontrar el objetivo del Disparador 'globalRotation'. (El objetivo debe aparecer antes de cualquier Setter, Trigger o Condition que lo use.) "

error. ¿Lo que pasa? Supongo que Storyboard.TargetName se evalúa durante el tiempo de ejecución, así que déjame compilarlo. ¿Es correcto?

¿Cómo hacer que funcione solo con un setter sin usar un guión gráfico?

Respuesta

10

El problema es que Trigger Setter solo puede orientar los objetos FrameworkElement o FrameworkContentElement, mientras que Storyboard funciona con cualquier DependencyProperty.

Aquí es una solución:

<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> 
Cuestiones relacionadas