1

I have a Canvas with an Ellipse object named Marker:

<!-- Boilerplate -->
<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:WpfApplication6="clr-namespace:WpfApplication6"
        Title="MainWindow" Height="350" Width="525"
        SnapsToDevicePixels="True">
  <Grid>
    <Canvas x:Name="DrawingCanvas">
      <!-- ##### Item of interest, below ##### -->
      <Ellipse x:Name="Marker" Canvas.Left="200" Canvas.Top="100" 
               Width="25" Height="25" Stroke="Black" Fill="#E0E000">
        <Ellipse.Effect>
          <BlurEffect x:Name="MarkerBlurEffect" Radius="0.0" />
        </Ellipse.Effect>
      </Ellipse>
    </Canvas>
  </Grid>
</Window>

I am trying to programatically give the Marker ellipse a blur animation:

  DoubleAnimation blurEffectAnimation=new DoubleAnimation
  {
    From=0, 
    To=10, 
    Duration=TimeSpan.FromSeconds(2.0)
  };

  // If I uncomment the line below, the blur effect animation works perfectly
  //Marker.Effect.BeginAnimation(BlurEffect.RadiusProperty, blurEffectAnimation); 

  // If I do it using the storyboard code below, the animation has no effect
  Storyboard.SetTarget(blurEffectAnimation, Marker.Effect);
  Storyboard.SetTargetProperty(blurEffectAnimation, 
                               new PropertyPath(BlurEffect.RadiusProperty));

  Storyboard sb=new Storyboard();

  sb.Children.Add(blurEffectAnimation);
  sb.Begin();

Interestingly, if I register the effect by name and use that name with the storyboard, the effect starts working again:

  // Register the MarketBlurEffect (as it's named in the XAML) effect by name 
  // with the FrameworkElement being animated (why do I need to?)
  Marker.RegisterName("MarkerBlurEffect",MarkerBlurEffect);

  // Instead of SetTarget, use SetTargetName on the registered name
  Storyboard.SetTargetName(blurEffectAnimation, "MarkerBlurEffect");
  Storyboard.SetTargetProperty(blurEffectAnimation, 
    new PropertyPath(BlurEffect.RadiusProperty));

  Storyboard sb=new Storyboard();

  sb.Children.Add(blurEffectAnimation);
  // Provide Marker to the Begin function in order to specify the name scope 
  // for the registered ("MarkerBlurEffect") name. Not doing this will result
  // in an InvalidOperationException with the Message property set to:
  //   No applicable name scope exists to resolve the name 'MarkerBlurEffect'.
  sb.Begin(Marker);
H.B.
  • 166,899
  • 29
  • 327
  • 400
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181

1 Answers1

1

Yep... you're right. As the Effect object is somehow deep in the tree you have to do that to access it. Or you can access your effect from the ellipse and change your property path to something like this:

DoubleAnimation blurEffectAnimation = new DoubleAnimation
{
  From = 0,
  To = 10,
  Duration = TimeSpan.FromSeconds(2.0)
};

Storyboard.SetTarget(
  blurEffectAnimation, 
  Marker);

Storyboard.SetTargetProperty(
  blurEffectAnimation,
  new PropertyPath("(Effect).Radius"));

Storyboard sb = new Storyboard();

sb.Children.Add(blurEffectAnimation);
sb.Begin();
NestorArturo
  • 2,476
  • 1
  • 18
  • 21
  • 1
    Well that's interesting, you set the target to be the ellipse shape (Marker) rather than the ellipse shape's Effect property. Then you referred to the property via a more complex path that mentions both the Effect property of the shape and its Radius property. Your approach certainly did work, so +1 for now. This makes it seem that the SetTarget function of Storyboard wants a FrameworkElement and not just a dependency property, despite the SetTarget function's prototype leading one to believe otherwise. – Michael Goldshteyn Jan 13 '12 at 16:22
  • 1
    I just tried the same thing with the RadiusX property of an EllipseGeometry in a path. Doesn't work with target Path.Data and property RadiusX, but works with target Path and property (Data).RadiusX. I agree with the explanation of SetTarget expecting a FrameworkElement, or al least UIElement. I think it hasn't got to do with the depth of the logical or viusal tree. – Clemens Jan 13 '12 at 16:39
  • 1
    More info is [here](http://stackoverflow.com/questions/2957582/wpf-storyboard-settarget-vs-storyboard-settargetname) and [here](http://stackoverflow.com/questions/2338714/why-dont-these-animations-work-when-im-using-a-storyboard) – Clemens Jan 13 '12 at 16:47
  • Actually this http://stackoverflow.com/questions/2338714/why-dont-these-animations-work-when-im-using-a-storyboard is a direct answer to why the by name method works and the by property way does not. – Michael Goldshteyn Jan 13 '12 at 17:11