WPF开发技巧:在WPF的DataGrid中实现动态增加列并刷新表格

1.VM代码

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Newtonsoft.Json;
using Prism.Commands;
using System.Data;
using System.Windows.Controls;

namespace Prism_AOI.Modules.SystemSettingModule.ViewModels
{
    public class CameraSettingViewModel : BindableBase
    {
        private ObservableCollection _testList;

        public ObservableCollection TestList
        {
            get { return _testList; }
            set { SetProperty(ref _testList, value); }
        }
        private ICommand _addCommand;
        public ICommand AddCommand
        {
            get { return _addCommand; }
            set { SetProperty(ref _addCommand, value); }
        }
        public CameraSettingViewModel()
        {
            _addCommand = new DelegateCommand(AddTest);
            _dataGridLoadedCommand = new DelegateCommand(GetDatagrid);
        }

        /// 
        /// 用于获取表格
        /// 
        /// 
        private void GetDatagrid(object grid)
        {
            TestGirdView = grid as DataGrid;
            init();
        }

        private DataTable _sourceTable = new DataTable();

        public DataTable SourceTable
        {
            get { return _sourceTable; }
            set { SetProperty(ref _sourceTable, value); }
        }

        private ICommand _dataGridLoadedCommand;

        public ICommand DataGridLoadedCommand
        {
            get { return _dataGridLoadedCommand; }
            set { SetProperty(ref _dataGridLoadedCommand, value); }
        }

        private DataGrid _testGirdView;

        public DataGrid TestGirdView
        {
            get { return _testGirdView; }
            set { SetProperty(ref _testGirdView, value); }
        }



        private void init()
        {
            #region 用于初始化表格数据,但是没办法动态添加列,适用于一开始就确定好表格有多少列的形式
            //_testList = new ObservableCollection();
            //List> temp = new List>();
            //for (int i = 0; i < 10; i++)
            //{
            //    Dictionary dic = new Dictionary();
            //    for (int j = 0; j < 10; j++)
            //    {
            //        //temp.Add( new Dictionary(){{"", "" },{ "", "" } });
            //        dic.Add("名字" + j, i + j.ToString());
            //    }
            //    temp.Add(dic);
            //}
            //_testList = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(temp));

            #endregion
            //这里为新添加的行数据,Key对应列头,value对应列名称

            List> dictionaries= new List>();

            //Dictionary temp = new Dictionary();
            for (int i = 0; i < 10; i++)
            {
                Dictionary dic = new Dictionary();
                for (int j = 0; j < 10; j++)
                {
                    dic.Add("名字" +i+ j, i + j.ToString());
                }
                dictionaries.Add(dic);
            }
            //需要先遍历添加所有不存在的列头!!!
            foreach (var dictionary in dictionaries)
            {
                foreach (var res in dictionary)
                {
                    //如果列头不存在则进行列头的添加

                    if (!_sourceTable.Columns.Contains(res.Key))
                    {
                        _sourceTable.Columns.Add(res.Key);
                    }
                }
            }
            foreach (var dictionary in dictionaries)
            {
                DataRow row = null;
                //创建行:此时已经包含之前没有的列
                row = _sourceTable.NewRow();
                foreach (var res in dictionary)
                {
                    row[res.Key] = res.Value; //对应的单元格赋值(通过键值对的方式添加,要保证键值对不能重复)
                }
                SourceTable.Rows.Add(row);
            }
            TestGirdView.ItemsSource = null;
            TestGirdView.ItemsSource = _sourceTable.DefaultView;

        }

        private void AddTest()
        {
            //_sourceTable.Columns.Add(new DataColumn("Column2"));
            //如果不重新绑定数据源的话没办法更新列
            TestGirdView.ItemsSource = null;
            TestGirdView.ItemsSource = _sourceTable.DefaultView;
        }
    }
}
 
  

 2.界面布局样式


    
        
            
        
        
            
            
        
        
            
            
        
        
        
    
    
        
            
        
    

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