wpf DataGrid的操作

[本篇内容来自网络] 

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;
        }

posted @ 2011-12-07 17:21  西天之旅  阅读(1385)  评论(0编辑  收藏  举报