WPF Grid + DataGrid多表头 动态列实现

使用了Grid + DataGrid 显示

Grid部分(表头部分)用了GridSplitter作为列的区分,可以调节列宽,(未实现自动排序)

DataGrid部分,隐藏DataGrid的Header部分

界面部分:


                
                    
                    
                
                
                   
                
            
            
        

主要作用是确定名称和一些基本设置,当然后台也可以。

 

动态列实现是由ObservableCollection>模拟,用于数据更新时的局部刷新。

3行4列,表头每列的显示有边框可以去掉,只用GridSplitter作为分割线即可。即在创建表头时需要自己算一下GridSplitter的行列关系。

风格不会调。。。。。

WPF Grid + DataGrid多表头 动态列实现_第1张图片

 

后台代码:

public MainWindow()
        {
            InitializeComponent();
	   BuildDG2();
            BuildDG21();
        }
public ObservableCollection> datas2 = new ObservableCollection>();

        void BuildDG2()
        {
            for (int i = 0; i < 4; i++)
            {
                ColumnDefinition column1 = new ColumnDefinition();
                column1.Width = GridLength.Auto;
                this.GridHs.ColumnDefinitions.Add(column1);

                ColumnDefinition column = new ColumnDefinition();
                column.Width = new GridLength(1);
                this.GridHs.ColumnDefinitions.Add(column);
            }

            for (int i = 0; i < 3; i++)
            {
                this.GridHs.RowDefinitions.Add(new RowDefinition());
                RowDefinition column = new RowDefinition();
                column.Height = new GridLength(1);
                this.GridHs.RowDefinitions.Add(column);
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (i == 0 && j == 2)
                    {
                        continue;
                    }
                    UniformGrid gridTitle = new UniformGrid();

                    Border b = new Border();
                    b.VerticalAlignment = VerticalAlignment.Stretch;
                    b.HorizontalAlignment = HorizontalAlignment.Stretch;
                    b.Name = "Grid" + i + j;

                    Pen p = new Pen();
                    
                    b.BorderBrush = new SolidColorBrush(Colors.Green);
                    b.BorderThickness = new System.Windows.Thickness(2);
                   
                    

                    TextBlock tbxTitleChinese = new TextBlock();

                    b.Child = tbxTitleChinese;

                    tbxTitleChinese.Text = "语文" + i + j;
                    tbxTitleChinese.Width = 60;
                    tbxTitleChinese.Height = 20;

                    gridTitle.Children.Add(b);

                    gridTitle.SetValue(Grid.RowProperty, i*2);
                    gridTitle.SetValue(Grid.ColumnProperty, j*2);
                    this.GridHs.Children.Add(gridTitle);

                    if (i == 0 && j == 1)
                    {
                        Grid.SetColumnSpan(gridTitle,3);
                        b.Width = 100;
                    }

                    if (i==2)
                    {
                        Binding bd = new Binding();
                        bd.Path = new System.Windows.PropertyPath("Width");
                        bd.ElementName = @"行" + i;
                        BindingOperations.SetBinding(gridTitle, WidthProperty, bd);
                    }
                }
            }

            for (int i = 0; i < 4; i++)
            {
                GridSplitter gSp = new GridSplitter();
                gSp.Background = new SolidColorBrush(Colors.Green);
                gSp.HorizontalAlignment = HorizontalAlignment.Stretch;
                gSp.Width = 1;

                if (i == 1)
                {
                    gSp.SetValue(Grid.RowProperty, 1);
                }
                else
                {
                    gSp.SetValue(Grid.RowProperty, 0);
                }
                gSp.SetValue(Grid.ColumnProperty, i * 2 + 1);
                this.GridHs.Children.Add(gSp);
                Grid.SetRowSpan(gSp, 6);
            }
                    
        }
        void BuildDG21()
        {
            for (int i = 0; i < 10; i++)
            {
                ObservableCollection columnsList = new ObservableCollection();
                for (int j = 0; j < 4; j++)
                {
                    columnsList.Add((i + j + 3).ToString());
                }
                datas2.Add(columnsList);
            }

            for (int i = 0; i < 4; i++)
            {
                DataGridTextColumn dgColumn = new DataGridTextColumn();
                dgColumn.Header = @"行" + i;
                dgColumn.SetValue(NameProperty, @"行" + i);
                this.DataGridHs.Columns.Add(dgColumn);

                MultiBinding mbd = new MultiBinding();
                Binding bd1 = new Binding("[" + i + "]");


                Binding bd2 = new Binding();
                bd2.Path = new System.Windows.PropertyPath("ActualWidth");
                bd2.Source = LogicalTreeHelper.FindLogicalNode(this.GridHs,"Grid2" + i);           
               
                dgColumn.Binding = bd1;
                BindingOperations.SetBinding(dgColumn, DataGridTextColumn.WidthProperty, bd2);               
            }

            this.DataGridHs.DataContext = datas2; 
        }




 

      

你可能感兴趣的:(WPF,C#)