У меня странная модель данных, и я пытаюсь создать динамические столбцы в сетке данных и правильно связать элементы.
У меня есть список объектов Row, которые я хочу привязать к DataGrid и отобразить с помощью простого DataGridTextColumn.
<controls:DataGrid
Grid.Row="1"
x:Name="dataGrid"
ItemsSource="{x:Bind ViewModel.CurrentRows}"
Моя цель — получить список столбцов из первой строки и построить столбцы сетки при настройке привязок. У меня возникли проблемы с определением правильного способа привязки данных в RowValue.value.
public TablePage()
{
InitializeComponent();
dataGrid.ItemsSource = ViewModel.CurrentRows;
foreach (Column column in ViewModel.CurrentRows.FirstOrDefault().Values.Select(x => x.key))
{
dataGrid.Columns.Add(new DataGridTextColumn()
{
Header = column.ColumnValidation.column_label,
Binding = new Binding() { Path = new PropertyPath("Values.value") }
});
}
}
И в моей модели просмотра у меня есть:
public ObservableCollection<Row> CurrentRows
И объект Row выглядит так:
public class Row: INotifyPropertyChanged
{
public List<RowValue> Values { get; set; } = new List<RowValue>();
public event PropertyChangedEventHandler PropertyChanged;
}
И, наконец, объект RowValue выглядит так:
public class RowValue: INotifyPropertyChanged
{
public Column key { get; set; }
public string value { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
Колонка выглядит так:
public class Column
{
public string name;
public ColumnValidation ColumnValidation;
}
Проверка столбца выглядит следующим образом:
public class ColumnValidation
{
public string column_label;
public DataTypeEnum data_type;
public int width;
public int decimal_places;
public int display_order;
public string calculation_formula;
public string edit_style;
public string default_value;
public decimal? minimum_value;
public decimal? maximum_value;
public bool is_column_nullable = false;
public bool inherit_values = false;
public bool display_column = false;
public bool is_editable = false;
public int column_style;
public string code_table_name;
public string code_display_name;
public string code_data_column_name;
}
column_label
и помещения его в классRow
(вам необходимо изменить существующую структуру данных). На самом деле согласно текущей привязке (ObservableCollection‹Row›) можно отображать информацию только на уровне Row, а путь привязкиValues.value
не будет действовать. Посколькуvalue
является свойством классаRowValue
, а не свойствомList<RowValue>
- person Richard Zhang - MSFT   schedule 21.08.2020DataGridTextColumn
, необходимо указать фиксированный путь привязки. Если вы хотите использоватьDataGridTemplateColumn
, хотя он более настраиваемый, он может не удовлетворить ваши потребности в автоматическом создании имен столбцов. - person Richard Zhang - MSFT   schedule 24.08.2020