问题描述
你好,
我是一名新程序员,因此决定尝试使用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; }
}
}
推荐答案
这篇关于使用GridView获得简单的硬编码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!