如何移除WPF Canvas子元素的附加顶部/底部/左侧/右侧属性?

4
也许我在这里错过了一些简单的东西,但我找不到从Canvas中包含的对象中删除已附加属性的方法。
代码示例:
//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);

这不起作用是因为Top附加属性优先于Bottom附加属性,所以我们尝试“取消设置”Top附加属性。

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

编译器报错:无法将UnsetValue转换为double类型。

我错过了什么?我们如何移除Top附加属性?

2个回答

8

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

theImage.ClearValue(Canvas.TopProperty);

或者在DependencyObject的代码中,从自身移除该值:

ClearValue(Canvas.TopProperty, theImage);

1

来自Canvas.Top文档

默认值为NaN

尝试设置Canvas.SetTop(theImage, double.NaN);,这应该会有所帮助。


2
这在这种情况下可能有效,但通常不是一个好的解决方案。它既需要找到默认值,也会覆盖任何优先级低于本地(即继承或样式化值)的值确定方法。 - John Bowen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接