博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf动态创建DataGrid
阅读量:4926 次
发布时间:2019-06-11

本文共 2274 字,大约阅读时间需要 7 分钟。

  public DataGrid CreateDataGrid()

{
//自定义DataGrid
DataGrid dataGrid = null;
dataGrid = new DataGrid();

dataGrid.Height = 340;

dataGrid.Margin = new Thickness(10, 30, 0, 0);
dataGrid.IsReadOnly = true;
dataGrid.AutoGenerateColumns = false;
dataGrid.CanUserResizeColumns = false;
System.Windows.Data.Binding binding = null;
binding = new System.Windows.Data.Binding("E_ID");
binding.Mode = System.Windows.Data.BindingMode.OneWay;
DataGridTextColumn dgtcE_Id = null;
dgtcE_Id = new DataGridTextColumn();
dgtcE_Id.Header = "编号";
dgtcE_Id.Width = 70;
dgtcE_Id.Visibility = Visibility.Collapsed;
dgtcE_Id.Binding = binding;
dataGrid.Columns.Add(dgtcE_Id);
binding = new System.Windows.Data.Binding("E_Type");
binding.Mode = System.Windows.Data.BindingMode.OneWay;
DataGridTextColumn dgtcE_Type = null;
dgtcE_Type = new DataGridTextColumn();
dgtcE_Type.Header = "下拉框名称";
dgtcE_Type.Width = 200;
dgtcE_Type.Binding = binding;
dataGrid.Columns.Add(dgtcE_Type);
binding = new System.Windows.Data.Binding("E_TypeName");
binding.Mode = System.Windows.Data.BindingMode.OneWay;
DataGridTextColumn dgtcE_TypeName = null;
dgtcE_TypeName = new DataGridTextColumn();
dgtcE_TypeName.Header = "下拉框内容";
dgtcE_TypeName.Width = 200;
dgtcE_TypeName.Binding = binding;
dataGrid.Columns.Add(dgtcE_TypeName);
binding = new System.Windows.Data.Binding("状态");
binding.Mode = System.Windows.Data.BindingMode.OneWay;
DataGridTextColumn dgtcReveaState = null;
dgtcReveaState = new DataGridTextColumn();
dgtcReveaState.Header = "是否启用";
dgtcReveaState.Width = 120;
dgtcReveaState.Binding = binding;
dataGrid.Columns.Add(dgtcReveaState);

//DataGrid中Column的内容居中

Style styleRight = new Style(typeof(TextBlock));
Setter setRight = new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
styleRight.Setters.Add(setRight);
foreach (DataGridColumn c in dataGrid.Columns)
{
DataGridTextColumn tc = c as DataGridTextColumn;
if (tc != null)
{
tc.ElementStyle = styleRight;
}
}

//表头居中

Style style = new Style(typeof(DataGridColumnHeader));
setRight = new Setter(DataGridColumnHeader.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
style.Setters.Add(setRight);
dataGrid.ColumnHeaderStyle = style;

return dataGrid;

}

转载于:https://www.cnblogs.com/swarb/archive/2013/03/01/9924325.html

你可能感兴趣的文章
hbase
查看>>
用PHP将Unicode 转化为UTF-8
查看>>
HDOJ1002 A+B Problem II
查看>>
ADB server didn't ACK(adb不能开启
查看>>
网页内容抓取
查看>>
分布式和集群的区别
查看>>
Python基础(三)
查看>>
Sql server在cmd下的使用
查看>>
【BZOJ 1019】 1019: [SHOI2008]汉诺塔 (DP?)
查看>>
织梦DEDECMS系统中文章内容为空 用SQL语句如何删除?
查看>>
load data导入数据之csv的用法
查看>>
silverlight调用MVC WebApi方法
查看>>
建表sql实例
查看>>
区块链北大课程总结(课程1-密码学原理)
查看>>
web页面开发笔记(不断更新)
查看>>
<转>C#读取doc,pdf,ppt文件
查看>>
缓存帮助类
查看>>
VC++ 2010 创建高级Ribbon界面详解(1)
查看>>
未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序...
查看>>
JS基础(四)运算符
查看>>