我有一個DataGrid綁定到一個數據表,ComboBoxColumn。此列的XAML如下:WPF DataGrid中的ComboBox列與DataTable作爲ItemsSource
<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));
}
}
的數據表服務作爲項目源網格包含「EMPID」列和「RepName」列。組合框與我的所有員工一起填充,當我做出選擇時,它會反映在數據表中。但是,當屏幕加載時,組合框中默認未選擇當前分配的員工。我認爲組合框的SelectedValueBinding屬性會處理這個......我做錯了什麼?
更新澄清:
數據網格被綁定到一個數據表,它包括一個僱員柱。我們假設當屏幕加載時,表中有三行,EmployeeID爲1,2和3.我需要第一行中的組合框列選擇EmployeeID 1,第二行選擇EmployeeID 2,以及第三行選擇EmployeeID 3。
只是好奇,運行時輸出窗口中是否有任何數據綁定錯誤? – 2011-05-11 18:09:55
沒有數據綁定錯誤 – drowned 2011-05-11 20:45:34