WPF DataGrid columns表头根据数据集动态动态生成Demo

        思路是这样的,数组集合装表头的信息,遍历这个集合,遍历过程中处理一下数据,然后就把每表头信息添加到dataGrid2.Columns.Add(templateColumn);

1,页面Xaml代码:

  <DataGrid x:Name="dataGrid" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False" ColumnHeaderHeight="30" RowHeight="30"/>

2,声明一个类:

public class TY_DataGridColumsHeader
{
    public string Name { get; set; }

    public object HeaderTitle { get; set; }

    public object IsVisible { get; set; }
}

3,声明表头数据集合,再遍历。 

#region 自己创建的DataGrid表头
            var dpDescriptor2 = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(DataGrid.ItemsSourceProperty, typeof(DataGrid));

            ObservableCollection<TY_DataGridColumsHeader> listheadttile = new ObservableCollection<TY_DataGridColumsHeader>();
            listheadttile.Add(new TY_DataGridColumsHeader()
            {
                HeaderTitle = "名称",
                Name = "Name"
            });
            listheadttile.Add(new TY_DataGridColumsHeader()
            {
                HeaderTitle = "年龄",
                Name = "Age"
            });

            dpDescriptor2.AddValueChanged(dataGrid2, (s, e) => {
                dataGrid2.Columns.Clear();

                foreach (var item in listheadttile)
                {
                    var templateColumn = new DataGridTemplateColumn()
                    {
                        Header = item.HeaderTitle,
                        CellTemplate = (DataTemplate)XamlReader.Parse(GetCellTemplateXaml2(item.Name))
                    };
                    dataGrid2.Columns.Add(templateColumn);
                }


            });

#endregion

4,cell的模版 

public static string GetCellTemplateXaml2(string listheadttile)
        {
            var templateStr = "<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">";
            //templateStr += "<Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*" /><ColumnDefinition Width="Auto" /></Grid.ColumnDefinitions>";
            //templateStr += $"<UniformGrid Columns="{(hasFlag ? cellBindingList.Count - 1 : cellBindingList.Count)}">";
            //cellBindingList.ForEach(a =>
            //{
            //    if (!hasFlag || a != cellBindingList.Last())
            //    {
            //        templateStr += "<TextBlock Margin="10 0 10 0" Text="{Binding " + a + "}" VerticalAlignment="Center" HorizontalAlignment="Center" />";
            //    }
            //});
            //templateStr += "</UniformGrid>";
            //if (hasFlag)
            //{
            //    templateStr += "<TextBlock Grid.Column="1" Text="{Binding " + cellBindingList.Last() + "}" Width="50" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />";
            //}
            templateStr += "<Grid>";

            templateStr += "<TextBlock Margin="10 0 10 0" Text="{Binding " + listheadttile + "}" VerticalAlignment="Center" HorizontalAlignment="Center" />";

            templateStr += "</Grid></DataTemplate>";
            return templateStr;
        }