【www.gdgbn.com--Silverlight】
命名空间:xmlns:primitives="clr-namespace:system.windows.controls.primitives;assembly=system.windows.controls.data"
再通过定义统一资源样式
然后在要居中的列引用这个样式如
cellstyle="{staticresource datagridcellstyle}"
headerstyle="{staticresource datagridheaderstyle}"这个是列头
自定义表格
public class table:grid
{
#region table属性
private int rows;
private int columns;
private color tablecolor;public int rows {
get { return rows; }
set { rows = value; }
}public int columns {
get { return columns; }
set { columns = value; }
}public color tablecolor {
get { return tablecolor; }
set { tablecolor = value; }
}#endregion
#region table事件#endregion
#region table方法
public table()
{}
public table(int row, int couumn,color color)
{
rows = row;
columns = couumn;
tablecolor = color;
}///
/// 初始化
///
public void inittable() {border border = new border();
border.borderbrush = new solidcolorbrush(colors.black);
border.borderthickness = new thickness(2);
grid grid = new grid();for (int i = 0; i < rows; i++)
{
grid.rowdefinitions.insert(i, new rowdefinition() { height = new gridlength(1,gridunittype.star) });
}
for (int j = 0; j < columns; j++)
{
grid.columndefinitions.insert(j, new columndefinition() { width = new gridlength(1,gridunittype.star) });
}//添加矩阵到单元格中
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++) {
rectangle rec1 = getrectangle();
rec1.setvalue(grid.rowproperty, r);
rec1.setvalue(grid.columnproperty, c);
grid.children.add(rec1);
}
}
border.child = grid;
this.children.add(border);
}
rectangle getrectangle()
{
rectangle rectangle = new rectangle
{
horizontalalignment = system.windows.horizontalalignment.stretch,
verticalalignment = system.windows.verticalalignment.stretch,
margin = new thickness(0, 0, 0, 0),
stroke = new solidcolorbrush(colors.black),
};return rectangle;
}rectangle getrectangle(int r,int c,color color)
{
rectangle rectangle = new rectangle
{
horizontalalignment = system.windows.horizontalalignment.stretch,
verticalalignment = system.windows.verticalalignment.stretch,
margin = new thickness(0, 0, 0, 0),
stroke = new solidcolorbrush(color),
};return rectangle;
}#endregion
}
使用方法
table table = new table();
table.rows = int.parse(tbrow.text.trim());
table.columns = int.parse(tbcolumn.text.trim());
table.inittable();
gridcontent.children.add(table);