我有一个连接到SQL Server数据库的简单应用程序,我试图从服务器提取数据以将其添加到WPF ListView
。我已经完成了我认为很难的部分,并成功地提取了数据 - 已经证实了这一点。但是当我尝试将其添加到我现有的列表视图时,列表视图仍然是空白的。这是我要添加的代码。
GetEmployees
使用中间层类连接到数据库并检索Employee的属性。它成功创建了所有这些变量,并从数据库中分配它们的值。底线 - lvwEmpSearch.Items.Add(emp);
是不起作用的。
编辑:代码已调整。
添加到列表视图的代码:
public ObservableCollection<EmployeeViewModel> Employees { get; set; } = new ObservableCollection<EmployeeViewModel>();
private void btnFindAllEmployees_Click(object sender, RoutedEventArgs e)
{
List<Employee> empList = GetEmployees();
foreach (Employee emp in empList)
{
var model = new EmployeeViewModel
{
empID = emp._empID,
Name = emp._fName + " " + emp._lName,
Address = emp._address + ", " + emp._city + ", " + emp._state + " " + emp._zip,
HireDate = emp._doH,
Phone = emp._phone,
PayRate = emp._payRate,
Email = emp._email,
};
Employees.Add(model);
}
}
EmployeeViewModel类:
public class EmployeeViewModel
{
public int empID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime HireDate { get; set; }
public string Phone { get; set; }
public decimal PayRate { get; set; }
public string Email { get; set; }
}
答案 0 :(得分:2)
创建一个ViewModel来保存您想要显示的项目信息。
基于列绑定,您正在寻找类似
的内容public class EmployeeViewModel {
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public DateTime HireDate { get; set; }
public string Phone { get; set; }
public decimal PayRate { get; set; }
public string Email { get; set; }
}
创建一个可观察的集合来保存项目
public ObservableCollection<EmployeeViewModel> Employees { get; set; } = new ObservableCollection<EmployeeViewModel>();
并允许视图绑定到列表
<ListView x:Name="lvwEmpSearch" ItemsSource="{Binding Employees}" >
<!-- ...removed for brevity -->
</ListView>
现在所需要的只是在主ViewModel中填充集合
List<Employee> empList = GetEmployees();
foreach (Employee emp in empList) {
var model = new EmployeeViewModel {
ID = emp._empID,
Name = emp._fName + " " + emp._lName,
Address = emp._address + ", " + emp._city + ", " + emp._state + " " + emp._zip,
HireDate = emp._doH,
Phone = emp._phone,
PayRate = emp._payRate,
Email = emp._email,
};
Employees.Add(model);
}
您似乎正在执行代码中的所有操作,因此您需要绑定视图。最初的假设是您遵循MVVM模式。
//CTOR
public EmployeesView() {
this.InitializeComponents();
this.Employees = new ObservableCollection<EmployeeViewModel>();
//Bind the view so that
this.DataContext = this;
}
public ObservableCollection<EmployeeViewModel> Employees { get; private set; }
private void btnFindAllEmployees_Click(object sender, RoutedEventArgs e) {
//...code removed for brevity
}
答案 1 :(得分:0)
尝试执行以下操作:
在您看来:
{{ newNewsForm?.value?.content.replace('\n','<br/>') }}
在视图后面的代码中:
<ListView ItemsSource="{Binding Employees}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"></GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
MainViewModel代码:
public MainWindow() {
InitializeComponent();
DataContext = new MainViewModel();
}
答案 2 :(得分:-1)
你应该能够创建一个包含你的员工的ObservableCollection并将它们绑定到ItemsSource。
private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();
public ObservableCollection<Employee> Employees
{
get { return _employees; }
protected set
{
if (_employees == value)
return;
_employees = value;
OnPropertyChanged("Employees");
}
}
然后像这样绑定它:
<ListView ItemsSource="{Binding Employees}"/>
在代码中你可以填写:
Employees.AddRange(GetEmployees());