C# Wpf 后台代碼設定UIElement的Canvas位置屬性值


后台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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM