我有三个datagrids构建在三个相同类型的不同ObservableCollections上:
ObservableCollection<MachineOrder> preactive;
ObservableCollection<MachineOrder> active;
ObservableCollection<MachineOrder> inProduction;
MachineOrder包含我想以编程方式添加到网格的特征字典。
我是这样做的:
foreach (StringWrapper characteristic in viewModel.Characteristics)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = characteristic.Value;
column.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);
Binding binding = new Binding();
binding.FallbackValue = "kein Wert";
binding.Path = new PropertyPath("Charakteristics[" + characteristic.Value + "]");
column.Binding = binding;
if (dgPreactive.Columns.FirstOrDefault(x => x.Header == column.Header) == null)
dgPreactive.Columns.Add(column);
if (dgActive.Columns.FirstOrDefault(x => x.Header == column.Header) == null)
dgActive.Columns.Add(column);
if (dgInProduction.Columns.FirstOrDefault(x => x.Header == column.Header) == null)
dgInProduction.Columns.Add(column);
if (viewModel.AddedColumns == null)
viewModel.AddedColumns = new ObservableCollection<DataGridColumn>();
if (viewModel.AddedColumns.FirstOrDefault(x => x.Header == column.Header) == null)
viewModel.AddedColumns.Add(column);
}
viewModel.Characteristics
包含我想要显示的Dictionaryitems。
这对第一个网格很有用,但在dgActive.Columns.Add(column)
上我得到以下参数异常:
“带有标题{0}的DataGridColumn已经存在于DataGrid的Columns集合中.DataGrids不能共享列,也不能包含重复的列实例。”