我有一个绑定到数据表的数据网格,带有 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。
I've got a datagrid bound to a datatable, with a ComboBoxColumn. The XAML for this column is as follows:
<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" />
My Employees class:
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));
}
}
The DataTable serving as items source for the grid contains an "EmpId" column and a "RepName" column. The combobox is populated with all my employees, and when I make a selection, it is reflected in the datatable. However, when the screen loads, the currently assigned employee is not selected by default in the combobox. I thought that the SelectedValueBinding property of the combobox would handle this... what am I doing wrong?
Update for clarification:
The datagrid is bound to a datatable which includes an EmployeeID column. Let's assume that when the screen loads, there are three rows in the table with EmployeeIDs 1, 2, and 3. I need the combobox column in the first row to have EmployeeID 1 selected, the second row to have EmployeeID 2 selected, and the third row to have EmployeeID 3 selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,
ComboBox
默认情况下不会选择集合中的第一项?如果是这种情况,您应该将
ComboBox
上的属性IsSychronizedWithCurrentItem
设置为 true。有关Selector.IsSynchronizedWithCurrentItem
的信息 这里。...
嗯,不幸的是,经过进一步研究,
DataGridComboBoxColumn
没有IsSynchronizedWithCurrentItem
属性。 :/但是,您可以创建一个看起来像这样的DataGridTemplateColunm
:希望有帮助!
If I understand your problem correctly, the
ComboBox
doesn't select the first item in the collection by default?If that's the case, you should set the property
IsSychronizedWithCurrentItem
on theComboBox
to true. Info aboutSelector.IsSynchronizedWithCurrentItem
here....
Well, unfortunately upon further research,
DataGridComboBoxColumn
does not have aIsSynchronizedWithCurrentItem
property. :/ But, you can create aDataGridTemplateColunm
that looks something like this:Hope that helps!