'How do I fill a datagrid in my View with my datatable in my ViewModel

I am working on an MVVM WPF Application managing Users. I want to display all users from the Users Table in a datagrid in my UsersView. Here is my dataGrid

        <DataGrid Grid.Row="1" Grid.Column="0" Name="UsersDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding UsersList}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name"  Binding="{Binding UTI_PRENOM}"/>
            <DataGridTextColumn Header="Last name" Binding="{Binding UTI_NOM}" />
            <DataGridTextColumn Header="Login" Binding="{Binding UTI_LOGIN}" />
        </DataGrid.Columns>
    </DataGrid>

This is how I currently fill the data grid in my UsersView.cs:

public UsersView()
    {
        InitializeComponent();
        getAllUsersInDataGrid();
        
    }

    private void getAllUsersInDataGrid()
    {
        using (GestUserDbContext context = contextFactory.CreateDbContext())
        {
            var users =
                from UTILISATEUR in context.UTILISATEUR
                select new { UTILISATEUR.UTI_PRENOM, UTILISATEUR.UTI_NOM, UTILISATEUR.UTI_LOGIN };
            UsersListDataGrid.ItemsSource = users.ToList();
        }
    }

And I would like to dispose of the View from having this responsibility (respecting MVVM) by giving it to my UsersViewModel which is currently empty. How should I do that, I've tried to create a UsersList variable in the ViewModel and bind it thanks to itemsSource but it didn't work. Btw my view is in a ContentControl Content Binded to My ViewModel and it works for the other uses so, I don't think it's the issue.

Hope I made myself clear enough, Thanks in advance.

Edit: I wanna add that my main problem here is that I can't use UsersListDataGrid.ItemsSource = users.ToList(); In my ViewModel since there is no UsersListDataGrid here. I'm trying to pass data through binding (UsersList) in ViewModel. I hope this is not more confusing.



Solution 1:[1]

I figured out how to bind properly, the need to use properties etc.. which resolved my problem, I'll close the post so nobody could waste time answering me !

Solution 2:[2]

There is no problem with your binding, you can always check with mock objects, in case you are not using a unit test:

Say this is your entity class:

public class User
{
    public string? UTI_NOM { get; set; }
    public string? UTI_PRENOM { get; set; }
    public string? UTI_LOGIN { get; set; }
}

And this is the code to bind:

    public MainWindow()
    {
        InitializeComponent();
        getAllUsersInDataGrid();
    }

    private void getAllUsersInDataGrid()
    {
        List<User> usersContext = new()
        {
            new User { UTI_LOGIN = "Login1", UTI_NOM = "Nom1", UTI_PRENOM = "PreNom1" },
            new User { UTI_LOGIN = "Login2", UTI_NOM = "Nom2", UTI_PRENOM = "PreNom2" },
            new User { UTI_LOGIN = "Login3", UTI_NOM = "Nom3", UTI_PRENOM = "PreNom3" },
        };

        var users =
            from UTILISATEUR in usersContext
            select new { UTILISATEUR.UTI_PRENOM, UTILISATEUR.UTI_NOM, UTILISATEUR.UTI_LOGIN };

        UsersDataGrid.ItemsSource = users.ToList();
    }

The data is successfully binding:

Screenshot of WPF Window

So, probably you have a problem with your DbContext and fetching the data from the database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Une Jolie fleur
Solution 2