Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello,

I am a new programmer and decided to try some C# code in WPF using MVVM. What I want to do is simple, I think, but I am having some issues wrapping my head around binding to the GridView. What I want to do is create a hardcoded list (for starters)of people--for example employees. I want the list to be displayed in a grid with the following column headings firstName, lastName, and department.

In the spirit of MVVM I have three classes: the Model, ViewModel, and View. I have searched long and hard for solutions that are similar, and followed many to every detail, but my data is not showing up. Perhaps, if one of you more experienced can take a look, you can point out where I am going wrong.

MY VIEW

HTML
<window x:class="WpfApplication3.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525" IsEnabled="True">
        <listview itemssource="{Binding EmployeeData}">
        <listview.view>
            <gridview>
                <gridviewcolumn width="140" header="First Name">
                    DisplayMemberBinding="{Binding Path=firstName}"/>
                <gridviewcolumn width="140" header="Last Name">
                    DisplayMemberBinding="{Binding lastName}" />
                <gridviewcolumn width="140" header="Department">
                    DisplayMemberBinding="{Binding department}" />
            </gridviewcolumn></gridviewcolumn></gridviewcolumn></gridview>
        </listview.view>
    </listview>
</window>


MY VIEWMODEL

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication3.Model;
using System.Collections.ObjectModel;

namespace WpfApplication3.ViewModel
{
   
    public class EmployeeListing
    {
        List<employee> _EmployeeData = new List<employee>();
        
        public EmployeeListing()    
        {
            _EmployeeData.Add(new Employee()
            {
                firstName = "Jackie",
                lastName = "O",
                department = "IT"
            });
            _EmployeeData.Add(new Employee()
            {
                firstName = "Billy",
                lastName = "Bob",
                department = "Finance"
            });
        }
    }
}</employee></employee>


MY MODEL

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApplication3.Model
{
    public class Employee
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string department { get; set; }

        
    }
}
Posted
Comments
Wjousts 9-Aug-11 17:23pm    
I don't see an "EmployeeData" anywhere in your view, view model or model, but you've got it set as your itemsource for your list view. I also don't see where you are initializing your model or view model.
Are you missing some code in your snippets? Else, that's your problem.

1 solution

I don't see where EmployeeData exists as a binding. As a new programmer, I'd say you should learn C# first, then learn some WPF, and then worry about things like MVVM after that. The fact that everyone tries to learn everything at once is why there's so few real programmers in the current generation coming up.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900