2011-03-28 59 views
3

也許我在這裏錯過了一些簡單的東西,但我無法找到一種方法從畫布中包含的項目中刪除附加屬性。WPF 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); 

這不會起作用,因爲頂部附加屬性優先於底部的附加屬性;所以我們儘量「未設置」頂部附加屬性

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

...和編譯器抱怨UnsetValue不能轉換爲double。

我在這裏錯過了什麼,我們如何刪除頂部附加屬性?

回答

6

您可以刪除本地DepenendencyProperty值與ClearValue:

theImage.ClearValue(Canvas.TopProperty); 

或爲DependencyObject的代碼中,從自身刪除值:

ClearValue(Canvas.TopProperty, theImage); 
+0

完美;謝謝! – 2011-03-30 21:36:29

1

Canvas.Top documentation

默認值是NaN

嘗試設置Canvas.SetTop(theImage, double.NaN);,這必須幫助。

+2

這可能會在這種情況下工作,但不是一個很好的解決方案一般來說。它既要求找到默認值,也要優先於低於本地值的任何值的確定方法(即繼承或樣式值)。 – 2011-03-28 22:25:32