我試圖用鼠標和代碼移動和重新定位元素。但我想我錯過了什麼或者做錯了什麼。所以我建了一個小樣本應用程序。這只是一個空的WPF應用程序與此主窗口功能Canvas.SetTop(元素,位置)有什麼問題
public MainWindow()
{
InitializeComponent();
Label lText = new Label();
lText.Content = "this is my test label";
lText.Height = 50;
lText.Width = 50;
lText.Background = Brushes.Aqua;
// do I really need to do this?
lText.VerticalAlignment = System.Windows.VerticalAlignment.Top;
lText.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
// this part already fails
Canvas.SetTop(lText, 20);
Canvas.SetLeft(lText, 10);
this.Content = lText;
}
@Clemens:爲什麼發表評論 –