Search code examples
c#uwpdatatablewindows-community-toolkit

What would be the best option to display class properties in a Microsoft.Toolkit.Uwp.UI.Controls.DataGrid?


Simplifying my case, basically, I have the following classes where Types are read from a DB:

public class Type1
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
    }

    public class Type2
    {
        public int Property3 { get; set; }
        public int Property4 { get; set; }
    }

    public class Info
    {
        public Type1 Type1 { get; set; }
        public Type2 Type2 { get; set; }
    }

What I would like to do, is display the name and value of each Type property in a DataGrid using the Info class. What approach should I choose to do this?

My idea was to create a DataTable from the Info class and use that as an input for the DataGrid. Would this be the best approach?


Solution

  • Yes, the best approach would be to create a class with a property per column (name and value) that you want to display in the DataGrid and then create instances of this class based on your existing type data.

    You should transform your data model into a view model that the DataGrid understands how to present on the screen.