使用GridView获得简单的硬编码列表 [英] Using GridView for a simple Hard Coded List

查看:78
本文介绍了使用GridView获得简单的硬编码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是一名新程序员,因此决定尝试使用MVVM在WPF中尝试一些C#代码.我想我想做的很简单,但是我在绑定到GridView时遇到了一些问题.我想要做的是创建人员(例如员工)的硬编码列表(供初学者使用).我希望列表显示在具有以下列标题firstName,lastName和department的网格中.

按照MVVM的精神,我有三个类:Model,ViewModel和View.我经过漫长而艰苦的搜索,寻找相似的解决方案,并且在每个细节上都遵循了许多规则,但是我的数据没有显示出来.也许,如果您中有经验的人可以看一下,您可以指出我要去哪里.

我的看法

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

<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

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

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; }

        
    }
}

推荐答案

我看不到EmployeeData作为绑定存在的位置.作为一名新程序员,我会说您应该首先学习C#,然后学习一些WPF,然后再担心诸如MVVM之类的事情.每个人都试图一次学习所有东西的事实,这就是为什么当前一代真正的程序员如此之少的原因.
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.


这篇关于使用GridView获得简单的硬编码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆