我有一个绑定到数据表的数据网格,带有一个 ComboBoxColumn。此列的 XAML 如下:
<DataGridComboBoxColumn Header="Rep Name" SortMemberPath="RepName"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, Source={StaticResource EmployeeList}, Path=Employees}"
SelectedValueBinding="{Binding Mode=TwoWay, Path=EmpId}"
SelectedValuePath="EmpId" DisplayMemberPath="RepName" />
我的员工类:
public class EmployeeList : INotifyPropertyChanged
{
private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();
public EmployeeList()
{
...
}
public ObservableCollection<Employee> Employees
{
get { return _employees; }
set { _employees = value; NotifyPropertyChanged("Employees"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Employee : INotifyPropertyChanged
{
private int _id;
public int EmpId
{
get { return _id; }
set { _id = value; OnPropertyChanged("EmpId"); }
}
public string RepName { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
作为网格项目源的 DataTable 包含一个“EmpId”列和一个“RepName”列。组合框填充了我所有的员工,当我做出选择时,它会反映在数据表中。但是,当屏幕加载时,默认情况下不会在组合框中选择当前分配的员工。我认为组合框的 SelectedValueBinding 属性会处理这个......我做错了什么?
澄清更新:
数据网格绑定到包含 EmployeeID 列的数据表。假设当屏幕加载时,表中有 EmployeeID 为 1、2 和 3 的三行。我需要第一行中的组合框列选择 EmployeeID 1,第二行选择 EmployeeID 2,并且第三行选择了 EmployeeID 3。