本帖最后由 kk791159796 于 2011-01-18 18:17:42 编辑

解决方案 »

  1.   

    TextBox tb = new TextBox();
    canvas1.Children.Add(tb);
    tb.SetValue(Canvas.LeftProperty, 10d);
    类似这样。
      

  2.   

    SetValue第一个参数是该对象的依赖属性,你可以通过智能提示看看都有哪些,第二个参数就是该属性的值,注意数据类型要对。
    上面的例子是设置相对于该TextBox所在的Canvas的左边距。
      

  3.   

    private void CreateButton(int x, int y)
            {
                canvas1.Children.Clear();
                double width = (this.canvas1.ActualWidth - (x + 1) * 5) / x;
                double height = (this.canvas1.ActualHeight - (y + 1) * 5) / y;
                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        Button bt = new Button()
                        {
                            Width = width,
                            Height = height
                        };
                        Canvas.SetTop(bt, j * height + 5);
                        Canvas.SetLeft(bt, i * width + 5);
                        canvas1.Children.Add(bt); 
                    }
                }
            }
    StackPanel则是基于流模式的,它没有绝对定位的概念,一批控件要么从左到右排列,要么从上而下排列Grid与StackPanel也是一样的 
      

  4.   

    Orientation应该是容器类的属性。例如
    StackPanel sp = new StackPanel();
    sp.Orientation = Orientation.Horizontal;