后台in-code設定元件UIElement的Canvas位置屬性的兩種方法:
1.UIElement.SetValue()方法
uiElement.SetValue(Canvas.TopProperty, 100.0); uiElement.SetValue(Canvas.LeftProperty, 100.0);
2. Canvas.SetTop()方法 //Right\Buttom\Left同
Canvas.SetTop(uiElement, 100.0); Canvas.SetLeft(uiElement, 100.0);
值得注意的是:
如果需要來回反復設定UIElement的位置屬性,且定位點變化(左上、左下、右上、右下),需要將另兩個屬性置為NaN,否則意想不到的結果。
//來 uiElement.SetValue(Canvas.TopProperty, 100.0); uiElement.SetValue(Canvas.LeftProperty, 100.0); uiElement.SetValue(Canvas.RightProperty, Double.NaN); uiElement.SetValue(Canvas.ButtomProperty, Double.NaN); //回 uiElement.SetValue(Canvas.TopProperty, Double.NaN); uiElement.SetValue(Canvas.LeftProperty, Double.NaN); uiElement.SetValue(Canvas.RightProperty, 100.0); uiElement.SetValue(Canvas.ButtomProperty, 100.0);