2009-04-15 33 views

回答

6

您需要的控件添加到Canvas第一。

yourCanvas.Children.Add(mc) 
6

將控件放在畫布或網格中是一個兩步過程。

  1. 的控件添加到容器的子集
  2. 設置控件的位置的容器

你已經得到了第2步之內,但缺少第一。

對於帆布

Button childButton = new Button(); 
LayoutCanvas.Children.Add(childButton); 
Canvas.SetLeft(childButton, 120); 
Canvas.SetTop(childButton, 120); 

一格

Button childButton = new Button(); 
LayoutGrid.Children.Add(childButton); 
Grid.SetRow(childButton, 2); 
Grid.SetColumn(childButton, 2); 
+1

謝謝布拉德。我注意到我還需要設置控件的寬度和高度以使其可見。 – 2009-04-16 17:32:55

1
(在某些情況下更容易)

另一種方式這樣做是從UIElement側:

(controlItem as UIElement).SetValue(Canvas.TopProperty, topVal); 
    (controlItem as UIElement).SetValue(Canvas.LeftProperty, left); 
相關問題