Создание DataGrid
Код: Выделить всё
private void PopulateDataGrid(List fieldNames)
{
DataGrid.Columns.Clear();
// Add the "Name" column first
var nameColumn = new DataGridTextColumn
{
Header = "Name",
Binding = new System.Windows.Data.Binding("name")
};
DataGrid.Columns.Add(nameColumn);
// Add other columns
foreach (var fieldName in fieldNames)
{
var column = new DataGridTextColumn
{
Header = fieldName,
Binding = new System.Windows.Data.Binding($"Fields[{fieldName}]")
};
DataGrid.Columns.Add(column);
}
var recordViewModels = records.Select(r => new RecordViewModel(r)).ToList();
DataGrid.ItemsSource = recordViewModels;
LogToFile($"Populated DataGrid with {recordViewModels.Count} records.");
}
Код: Выделить всё
public class RecordViewModel
{
private readonly string logFilePath = "";
public Record Record { get; }
public string Name => Record.Name;
public Dictionary Fields => Record.Fields;
public RecordViewModel(Record record)
{
Record = record;
LogFields();
}
public object this[string fieldName]
{
get
{
LogToFile("IM INSIDE THE GET");
if (Fields.ContainsKey(fieldName))
{
var fieldValue = Fields[fieldName];
var fieldType = fieldValue.GetType().Name;
LogToFile($"Field: {fieldName}, Type: {fieldType}");
if (fieldValue.GetType() == typeof(FieldGroup))
{
LogToFile($"Field '{fieldName}' identified as FieldGroup using exact type match");
return ""; // FieldGroup is shown as blank
}
else if (fieldValue is FieldGroup fieldGroup)
{
LogToFile($"Field '{fieldName}' identified as FieldGroup using 'is' operator");
return ""; // FieldGroup is shown as blank
}
else if (fieldValue is List fieldList)
{
LogToFile($"Field '{fieldName}' identified as FieldList");
return string.Join(", ", fieldList); // FieldList as concatenated string
}
else
{
LogToFile($"Field '{fieldName}' is of type {fieldType}");
return fieldValue;
}
}
else
{
LogToFile($"Field not found: {fieldName}");
return null;
}
}
}
}
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/786 ... g-accessed