4

Perhaps I am missing something simple here, but I am unable to find a method to remove an attached property from a item contained by a canvas.

Code example:

//Add an image to a canvas; set the location to the top
theCanvas.Children.Add(theImage);
Canvas.SetTop(theImage, 0);

//Move the image to the bottom of the canvas
Canvas.SetBtoom(theImage, 0);

This doesn't work since the Top attached property takes precedence over Bottom attached property; so we try to "unset" the top attached property

//Move the image to the bottom of the canvas
Canvas.SetTop(theImage, DependencyProperty.UnsetValue);
Canvas.SetBtoom(theImage, 0);

...and the compiler complains that UnsetValue can't be converted to a double.

What am I missing here and how do we remove the Top attached property?

Robert Altman
  • 5,355
  • 5
  • 33
  • 51

2 Answers2

8

You can remove local DepenendencyProperty values with ClearValue:

theImage.ClearValue(Canvas.TopProperty);

or inside a DependencyObject's code to remove the value from itself:

ClearValue(Canvas.TopProperty, theImage);
John Bowen
  • 24,213
  • 4
  • 58
  • 56
1

From Canvas.Top documentation:

The default value is NaN.

Try setting Canvas.SetTop(theImage, double.NaN);, this must help.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 2
    This may work in this case but is not a good solution in general. It both requires finding the default value and also overrides any method of value determination with a lower priority than Local (i.e. inherited or Styled values). – John Bowen Mar 28 '11 at 22:25