[WPF] DataGrid 动态切换数据源 ,动态变化列


因为新项目类似功能界面太多,需要动态变化DataGrid的内容

先感谢两位博主: 山海雍和 https://www.cnblogs.com/maomiyouai/p/3594132.html 

        可爱的肥皂 https://blog.csdn.net/weixin_42084199/article/details/99701080

部分代码直接搬运的。。。。。。

1. XMAL


       
           
           
       
       
           

2. model    (最关键的就是这个DynamicObject,组长让我用反射,然后我找到了vs2010的DynamicObject)

public class User:DynamicObject
   {
       public string Name { get; set; }
       public string Age { get; set; }
       public string Birth { get; set; }
       public string Remark { get; set; }
   }
   public class Log : DynamicObject
   {
       public string Time { get; set; }
       public string Conetnt { get; set; }
       public string Remark { get; set; }
   }
 
public class DataGridColumnModel
   {
       public string TemplateType { get; set; }
       public string Header { get; set; }
       public string Binding { get; set; }
       public string Width { get; set; }
   }

3.cs  控制数据源切换以及列显示, 两个对象的remark字段都未显示

public partial class PageDemo: Page
    {
 
        public Dictionary> PageColumnConfig = new Dictionary>
        {
            {"Column1", new List {
                { new DataGridColumnModel { Binding="Name", Header="姓名", Width="200"}},
                { new DataGridColumnModel { Binding="Age", Header="年纪", Width="200"} },
                { new DataGridColumnModel { Binding="Birth", Header="生日", Width="200"} },
            } },
            {"Column2", new List {
                { new DataGridColumnModel { Binding="Time", Header="时间", Width="200"}},
                { new DataGridColumnModel { Binding="Conetnt", Header="内容", Width="200"} }
            } },
        };
 
 
        public Dictionary> DataMap = new Dictionary>
        {
            { "Column1", new List() },
            { "Column2", new List() },
        };
        public RegisterAnalysis()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                dynamic model = new User();
                model.Name = "123";
                model.Age = "234";
                model.Birth = "345";
                model.Remark = "345";
                DataMap["Column1"].Add(model);
            }
 
            for (int i = 0; i < 10; i++)
            {
                dynamic model = new Log();
                model.Time = "aaaa";
                model.Conetnt = "bbbbb";
                model.Remark = "cccc";
                DataMap["Column2"].Add(model);
            }
 
            ShowColumns("Column1");
 
        }
 
 
        public void ShowColumns(string column)
        {
 
            DataGridContent.ItemsSource = DataMap[column];
 
            DataGridContent.Columns.Clear();
            var config = PageColumnConfig[column];
            foreach (var item in config)
            {
                Binding binding = new Binding();
                if (!string.IsNullOrWhiteSpace(item.Binding))
                    binding.Path = new PropertyPath(item.Binding);
                if (string.IsNullOrWhiteSpace(item.TemplateType))
                {
                    DataGridTextColumn dataGridTextColumn = new DataGridTextColumn();
                    dataGridTextColumn.Header = item.Header;
                    dataGridTextColumn.Width = new DataGridLength(Convert.ToDouble(item.Width));
                    if (!string.IsNullOrWhiteSpace(item.Binding))
                        dataGridTextColumn.Binding = new Binding(item.Binding);
                    DataGridContent.Columns.Add(dataGridTextColumn);
                }
                else
                {
                    DataGridTemplateColumn dataGridTemplateColumn = new DataGridTemplateColumn();
                    dataGridTemplateColumn.Header = item.Header;
                    dataGridTemplateColumn.CellTemplate = (DataTemplate)DataGridContent.FindResource(item.TemplateType);
                    DataGridContent.Columns.Add(dataGridTemplateColumn);
                }
            }
        }
 
        private void Column1_Click(object sender, RoutedEventArgs e)
        {
            ShowColumns("Column1");
        }
 
        private void Column2_Click(object sender, RoutedEventArgs e)
        {
            ShowColumns("Column2");
        }
 
    }
}

4.运行截图