Search code examples
wpfcanvasattached-properties

How can a WPF Canvas child's attached top/bottom/left/right property be removed?


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?


Solution

  • 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);