Chinaunix首页 | 论坛 | 博客
  • 博客访问: 683488
  • 博文数量: 103
  • 博客积分: 2532
  • 博客等级: 大尉
  • 技术积分: 2039
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-26 16:07
文章分类

全部博文(103)

文章存档

2012年(38)

2011年(28)

2010年(16)

2009年(16)

2008年(5)

分类: 嵌入式

2011-11-26 09:16:53

  1. public DataGrid CreateDataGrid()
  2.         {
  3.  //自定义DataGrid
  4.             DataGrid dataGrid = null;
  5.             dataGrid = new DataGrid();

  6.             dataGrid.Height = 340;
  7.             dataGrid.Margin = new Thickness(10, 30, 0, 0);
  8.             dataGrid.IsReadOnly = true;
  9.             dataGrid.AutoGenerateColumns = false;
  10.             dataGrid.CanUserResizeColumns = false;
  11.             System.Windows.Data.Binding binding = null;
  12.             binding = new System.Windows.Data.Binding("E_ID");
  13.             binding.Mode = System.Windows.Data.BindingMode.OneWay;
  14.             DataGridTextColumn dgtcE_Id = null;
  15.             dgtcE_Id = new DataGridTextColumn();
  16.             dgtcE_Id.Header = "编号";
  17.             dgtcE_Id.Width = 70;
  18.             dgtcE_Id.Visibility = Visibility.Collapsed;
  19.             dgtcE_Id.Binding = binding;
  20.             dataGrid.Columns.Add(dgtcE_Id);
  21.             binding = new System.Windows.Data.Binding("E_Type");
  22.             binding.Mode = System.Windows.Data.BindingMode.OneWay;
  23.             DataGridTextColumn dgtcE_Type = null;
  24.             dgtcE_Type = new DataGridTextColumn();
  25.             dgtcE_Type.Header = "下拉框名称";
  26.             dgtcE_Type.Width = 200;
  27.             dgtcE_Type.Binding = binding;
  28.             dataGrid.Columns.Add(dgtcE_Type);
  29.             binding = new System.Windows.Data.Binding("E_TypeName");
  30.             binding.Mode = System.Windows.Data.BindingMode.OneWay;
  31.             DataGridTextColumn dgtcE_TypeName = null;
  32.             dgtcE_TypeName = new DataGridTextColumn();
  33.             dgtcE_TypeName.Header = "下拉框内容";
  34.             dgtcE_TypeName.Width = 200;
  35.             dgtcE_TypeName.Binding = binding;
  36.             dataGrid.Columns.Add(dgtcE_TypeName);
  37.             binding = new System.Windows.Data.Binding("状态");
  38.             binding.Mode = System.Windows.Data.BindingMode.OneWay;
  39.             DataGridTextColumn dgtcReveaState = null;
  40.             dgtcReveaState = new DataGridTextColumn();
  41.             dgtcReveaState.Header = "是否启用";
  42.             dgtcReveaState.Width = 120;
  43.             dgtcReveaState.Binding = binding;
  44.             dataGrid.Columns.Add(dgtcReveaState);

  45.  //DataGrid中Column的内容居中
  46.             Style styleRight = new Style(typeof(TextBlock));
  47.             Setter setRight = new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center);
  48.             styleRight.Setters.Add(setRight);
  49.             foreach (DataGridColumn c in dataGrid.Columns)
  50.             {
  51.                 DataGridTextColumn tc = c as DataGridTextColumn;
  52.                 if (tc != null)
  53.                 {
  54.                     tc.ElementStyle = styleRight;
  55.                 }
  56.             }

  57.  //表头居中
  58.             Style style = new Style(typeof(DataGridColumnHeader));
  59.             setRight = new Setter(DataGridColumnHeader.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
  60.             style.Setters.Add(setRight);
  61.             dataGrid.ColumnHeaderStyle = style;

  62.             return dataGrid;
  63.         }
阅读(7320) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~