WPF中动态添加控件,以及设置他们的位置 本帖最后由 kk791159796 于 2011-01-18 18:17:42 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TextBox tb = new TextBox();canvas1.Children.Add(tb);tb.SetValue(Canvas.LeftProperty, 10d);类似这样。 SetValue第一个参数是该对象的依赖属性,你可以通过智能提示看看都有哪些,第二个参数就是该属性的值,注意数据类型要对。上面的例子是设置相对于该TextBox所在的Canvas的左边距。 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也是一样的 Orientation应该是容器类的属性。例如StackPanel sp = new StackPanel();sp.Orientation = Orientation.Horizontal; 关于类型转换 调试状态下运行正确,一旦改为start without debugging就报错 怎么调用软键盘? C# 点击源代码 直接运行程序-一个实用的C#工具(包括源码) 高手看过来 新手提问-关于菜单 C#一个句子出错,请教这是什么 c#怎么设置解决方案的程序语言是中文还是英文 急啊,请问如何统计网站的总访问量和今日访问量?高手们请帮忙,先谢了,在线等... 求一正则表达式 自动匹配查找算法
canvas1.Children.Add(tb);
tb.SetValue(Canvas.LeftProperty, 10d);
类似这样。
上面的例子是设置相对于该TextBox所在的Canvas的左边距。
{
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也是一样的
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;