I'm playing around with Avalonia, and got stuck trying to bind a DataGrid control to a DataTable.
In WPF binding the DataGrid to the table's DefaultView property causes the table's data to display. The Avalonia DataGrid just shows the properties of the row object.
It would be a nice feature to have if it would know by default how to bind to a DataTable (or really the DataView attached to the table).
I suspect there could be a possible workaround, defining a DataTemplate, but I haven't figured it out yet. Any tips would be welcome.
For anyone struggling with the same problem, here's the workaround I came up with:
public DataView DataView
{
get => _dataTable.DefaultView;
}
<DataGrid Name="MyGrid" Items="{Binding DataView}">
</DataGrid>
public MainWindow()
{
Activated += MainWindow_Activated; // <-- This line
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void MainWindow_Activated(object sender, System.EventArgs e)
{
var grid = this.FindControl<DataGrid>("MyGrid");
var vm = (MainWindowViewModel)DataContext;
var cols = vm.DataView.Table.Columns;
for (var i = 0; i < cols.Count; i++)
{
grid.Columns.Add(new DataGridTextColumn
{
Header = cols[i].ColumnName,
Binding = new Binding($"Row.ItemArray[{i}]"),
});
}
}
Most helpful comment
For anyone struggling with the same problem, here's the workaround I came up with: