-2

I am quite new to avalonia and have been tasked with converting a C# winforms application over to Avalonia, and I've hit a bump.
I'm creating multiple DataGrids dynamically (one datagrid for each student schedule), so this is done in the code as below:

for (int i = 0; i < studentcount; i++) {
    DataGrid dg = new DataGrid();
    var date = new DataGridTextColumn();
    date.Header = "Date";
    // date.Binding = new Binding("date"); ?
    dg.Columns.Add(date);
    canvas.Children.Add(dg);

}

From what i've seen through WPF, it is possible to bind each column to a struct property, with

date.Binding = new Binding("date");

(where date is a string property of a struct), so this way I can add items as

dg.Items.Add(new Student { date = "1/1/2022" });

However from what i've seen, as avalonia bindings work different to WPF, this isn't possible to add a binding in code as above. What alternative is available, or is there a way to work around this?

Cheers

1 Answers1

-1

Stop everything you're doing.

Go and spend a day learning about MVVM. If you carry on down this path you're creating a bit of a nightmare for yourself and future maintainers.

Your DataGrids should be created in XAML within a DataTemplate.

You can use an ItemsRepeater (link) bound to a data collection that is in turn using a DataTemplate with a DataGrid bound to the contents of each item within your collection.

Don't use Structs for plain UI data objects unless there is a explicit need or requirement.

Jammer
  • 9,969
  • 11
  • 68
  • 115