Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 239
Robert Altman Avatar asked Sep 12 '25 08:09

Robert Altman


2 Answers

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);
like image 199
John Bowen Avatar answered Sep 13 '25 23:09

John Bowen


From Canvas.Top documentation:

The default value is NaN.

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

like image 35
Vlad Avatar answered Sep 14 '25 01:09

Vlad