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<dynamic> _testList;
public ObservableCollection<dynamic> 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<object>(GetDatagrid);
}
/// <summary>
/// 用于获取表格
/// </summary>
/// <param name="grid"></param>
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<dynamic>();
//List<Dictionary<string, string>> temp = new List<Dictionary<string, string>>();
//for (int i = 0; i < 10; i++)
//{
// Dictionary<string, string> dic = new Dictionary<string, string>();
// for (int j = 0; j < 10; j++)
// {
// //temp.Add( new Dictionary<string, string>(){{"", "" },{ "", "" } });
// dic.Add("名字" + j, i + j.ToString());
// }
// temp.Add(dic);
//}
//_testList = JsonConvert.DeserializeObject<ObservableCollection<dynamic>>(JsonConvert.SerializeObject(temp));
#endregion
//这里为新添加的行数据,Key对应列头,value对应列名称
List<Dictionary<string, string>> dictionaries= new List<Dictionary<string, string>>();
//Dictionary<string, string> temp = new Dictionary<string, string>();
for (int i = 0; i < 10; i++)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
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.界面布局样式
<UserControl x:Class="Prism_AOI.Modules.SystemSettingModule.Views.CameraSetting"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Prism_AOI.Modules.SystemSettingModule.Views"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism ="http://prismlibrary.com/"
Background="MediumSlateBlue"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Width="100" Command="{Binding AddCommand }">添加</Button>
<Button Width="100">删除</Button>
</StackPanel>
<DataGrid Name="gridView" Grid.Row="1" IsReadOnly="True" CanUserAddRows="True" ItemsSource="{Binding SourceTable ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged }">
</DataGrid>
</Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<prism:InvokeCommandAction Command="{Binding DataGridLoadedCommand }" CommandParameter="{Binding ElementName=gridView }"></prism:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</UserControl>