如何删除WPF Canvas子项的附加顶部/底部/左/右属性?

Rob*_*man 3 wpf canvas attached-properties

也许我在这里遗漏了一些简单的东西,但是我无法找到从画布所包含的项中删除附加属性的方法.

代码示例:

//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);
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为Top附加属性优先于Bottom附加属性; 所以我们试图"取消"顶部附属物

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

...并且编译器抱怨UnsetValue无法转换为double.

我在这里缺少什么,我们如何删除Top附加属性?

Joh*_*wen 7

您可以使用ClearValue删除本地DepenendencyProperty值:

theImage.ClearValue(Canvas.TopProperty);
Run Code Online (Sandbox Code Playgroud)

或者在DependencyObject的代码中删除自身的值:

ClearValue(Canvas.TopProperty, theImage);
Run Code Online (Sandbox Code Playgroud)