一、创建相关对象,并定义Grid中的列属性
Grid forwardAndReply = new Grid();//创建Grid对象
forwardAndReply.Margin = new Thickness(0, 10, 0, 5);
forwardAndReply.HorizontalAlignment = HorizontalAlignment.Right;
forwardAndReply.Height = 30;
//创建第一个ColumnDefinition
ColumnDefinition firstColumnDefinition = new ColumnDefinition();//定义Grid列属性对象
firstColumnDefinition.Width = new GridLength(125, GridUnitType.Pixel);//初始化GridLength并设置指定的值
forwardAndReply.ColumnDefinitions.Add(firstColumnDefinition);//添加ColumnDefinition到Grid的列属性集合中
//创建第二个ColumnDefinition
ColumnDefinition secondColumnDefinition = new ColumnDefinition();
secondColumnDefinition.Width = new GridLength(125, GridUnitType.Pixel);
forwardAndReply.ColumnDefinitions.Add(secondColumnDefinition);
二、将UI元素添加到指定的列中
RichTextBox content = new RichTextBox();//创建RichTextBox控件对象
Grid.SetColumn(forward,0);//设置即将要添加的UI元素所在的列,0为第一列
forwardAndReply.Children.Add(forward);//添加UI元素到Grid中