2009-09-15 8 views
5

Ho un progetto WPF con un bordo che utilizza il seguente stile. Il piano è di far sbiadire l'effetto bagliore quando il mouse si sposta oltre il bordo e scompare quando esce.Come animare Opacity di DropShadowEffect?

<Style x:Key="BorderGlow" TargetType="Border"> 
    <Style.Resources> 
     <Storyboard x:Key="GlowOn"> 
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(DropShadowEffect.Opacity)"> 
       <SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="GlowOff"> 
      <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(DropShadowEffect.Opacity)"> 
       <SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/> 
      </DoubleAnimationUsingKeyFrames> 
     </Storyboard> 
    </Style.Resources> 

    <Setter Property="CornerRadius" Value="6,1,6,1" /> 
    <Setter Property="BorderBrush" Value="{StaticResource SelectedBorder}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Background" Value="{StaticResource DeselectedBackground}" /> 
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5" /> 
    <Setter Property="TextBlock.Foreground" Value="{StaticResource SelectedForeground}" /> 

    <Setter Property="RenderTransform"> 
     <Setter.Value> 
      <RotateTransform Angle="90"/> 
     </Setter.Value> 
    </Setter> 

    <Setter Property="Effect"> 
     <Setter.Value> 
      <DropShadowEffect ShadowDepth="0" BlurRadius="8" Color="#FFB0E9EF"/> 
     </Setter.Value> 
    </Setter> 

    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 

      <Trigger.EnterActions> 
       <BeginStoryboard Storyboard="{StaticResource GlowOn}"/> 
      </Trigger.EnterActions> 

      <Trigger.ExitActions> 
       <BeginStoryboard Storyboard="{StaticResource GlowOff}"/> 
      </Trigger.ExitActions> 

     </Trigger> 
    </Style.Triggers> 
</Style> 

Il problema è che non succede niente! L'animazione funziona se cambio "DropShadowEffect" in "UIElement" nello Storyboard TargetProperty, ma questo sfuma l'intero controllo.

Come faccio a dissolvere solo DropShadowEffect?

risposta

6

Un paio di punti da notare

1) È necessario essere l'obiettivo di una effettiva proprietà di Border - Sei in effetti cercando di indirizzare il valore (DropShadowEffect) della proprietà effetto, non la proprietà stessa.

2) È necessario ordinare la sintassi dello PropertyPath.

Cambia la vostra proprietà Storyboard.Target al seguente e si dovrebbe andare bene:

Storyboard.TargetProperty="(Effect).Opacity" 

EDIT codice funzionante come già sottolineato in un commento:

<Border BorderThickness="10" Height="100" Width="100"> 
    <Border.BorderBrush> 
     <SolidColorBrush Color="Red"></SolidColorBrush> 
    </Border.BorderBrush> 
    <Border.Style> 
     <Style TargetType="Border"> 
      <Style.Resources> 
       <Storyboard x:Key="GlowOn"> 
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
          Storyboard.TargetProperty="(Effect).Opacity"> 
         <SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
       <Storyboard x:Key="GlowOff"> 
        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" 
          Storyboard.TargetProperty="(Effect).Opacity"> 
         <SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/> 
        </DoubleAnimationUsingKeyFrames> 
       </Storyboard> 
      </Style.Resources> 

      <Setter Property="CornerRadius" Value="6,1,6,1" /> 
     <!--<Setter Property="BorderBrush" 
        Value="{StaticResource SelectedBorder}" />--> 
      <Setter Property="BorderThickness" Value="1" /> 
     <!--<Setter Property="Background" 
        Value="{StaticResource DeselectedBackground}" />--> 
      <Setter Property="RenderTransformOrigin" Value="0.5,0.5" /> 
     <!--<Setter Property="TextBlock.Foreground" 
        Value="{StaticResource SelectedForeground}" />--> 

      <Setter Property="RenderTransform"> 
       <Setter.Value> 
        <RotateTransform Angle="90"/> 
       </Setter.Value> 
      </Setter> 

      <Setter Property="Effect"> 
       <Setter.Value> 
        <DropShadowEffect ShadowDepth="20" 
             BlurRadius="8" 
             Color="#FFB0E9EF"/> 
       </Setter.Value> 
      </Setter> 

      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 

        <Trigger.EnterActions> 
         <BeginStoryboard 
           Storyboard="{StaticResource GlowOn}"/> 
        </Trigger.EnterActions> 

        <Trigger.ExitActions> 
         <BeginStoryboard 
           Storyboard="{StaticResource GlowOff}"/> 
        </Trigger.ExitActions> 

       </Trigger> 
      </Style.Triggers> 

     </Style> 
    </Border.Style> 
</Border> 
+0

ottengo un InvalidOperationException perché Effetto doesn' t avere una proprietà Opacity. Il valore di Effect è DropShadowEffect che ha la proprietà Opacity che sto cercando di targetizzare, ma come fai notare il mio XAML è sbagliato. Ho provato a impostare x: Name su DropShadowEffect ma non puoi usare TargetName in Stili. C'è un buco nella documentazione MSDN sull'uso di queste parentesi nello storyboard, ma l'ho rintracciato nella guida di PropertyPath. Tuttavia non riesco ancora a risolvere il problema. Penso di aver bisogno di lanciare Effect su DropShadowEffect ma questo non sembra possibile in XAML. Qualche altra idea? – Zodman

+0

Sono riuscito a ottenere il codice che hai postato funzionante usando la sintassi che ho fornito, l'ho fatto in un'app campione spogliata. Pubblicherò quello che ho usato e forse puoi tornare indietro da quello ... –

+0

Nota Ho rimosso i setter che usano risorse statiche che non hai fornito ma che non effettueranno la dimostrazione. E noto che stai cercando di impostare TextBlock. Proprietà di primo piano nel tuo stile che non funzionerà. –