我是C#的新手,我通过WPF练习我的知识。我正在尝试创建一个" EmployeeDatabase"
public EmployeeDB()
{
_employeeList.Add(new EmployeeApp1.Employee() { Name = "Hecbert Doval", Email = "haloha@gmail.com", Salary = 14000 });
_employeeList.Add(new EmployeeApp1.Employee() { Name = "Harry Pottah", Email = "haloha@gmail.com", Salary = 653000 });
_employeeList.Add(new EmployeeApp1.Employee() { Name = "Amino Vikola", Email = "haloha@gmail.com", Salary = 230 });
_employeeList.Add(new EmployeeApp1.Employee() { Name = "Mila Spino", Email = "haloha@gmail.com", Salary = 30000 });
_employeeList.Add(new EmployeeApp1.Employee() { Name = "Tila Tequila", Email = "haloha@gmail.com", Salary = 90700 });
_employeeList.Add(new EmployeeApp1.Employee() { Name = "TheRock Johnson", Email = "haloha@gmail.com", Salary = 14500 });
}
private List<Employee> _employeeList;
使用&#34;员工列表&#34;对象和每个Employee对象都有&#34;姓名,薪水,电子邮件&#34;作为属性。
public class Employee
{
public string Name { get; set; }
public string Email { get; set; }
public int Salary { get; set; }
}
接下来,我创建了3个列表视图,将它们并排排列,每个列表视图都应该收到&#34; Name&#34; &#34;电子邮件&#34;和&#34;薪水&#34;单独。
<ListView Name="lvName" Margin="20" Grid.Column="0" />
<ListView Name="lvEmail" Margin="20" Grid.Column="1" />
<ListView Name="lvSalary" Margin="20" Grid.Column="2" />
我的问题是如何将每个ListView绑定到名称/电子邮件/工资?如果有可能..如果没有任何替代方案可以达到相同的结果? 提前谢谢!
答案 0 :(得分:0)
出于好奇,为什么你想要三个相邻的列表视图,每个只显示一个属性?
顺便问一下,您是否正在实施INotifyPropertyChanged
?你应该。
无论如何,这很容易。首先,公开_employeeList
:
private ObservableCollection<Employee> _employeeList
= new ObservableCollection<Employee>();
public ObservableCollection<Employee> EmployeeList { get { return _employeeList; } }
然后,绑定:
<ListView
ItemsSource="{Binding EmployeeList}"
DisplayMemberPath="Name"
Name="lvName"
Margin="20" Grid.Column="0"
/>
<ListView
ItemsSource="{Binding EmployeeList}"
DisplayMemberPath="Email"
Name="lvEmail"
Margin="20" Grid.Column="1"
/>
<ListView
ItemsSource="{Binding EmployeeList}"
DisplayMemberPath="Salary"
Name="lvSalary"
Margin="20" Grid.Column="2"
/>
如果您更愿意在代码隐藏中做所有事情,那么根据我向您展示的内容,您可以轻松找到自己。