我有一个问题,即实现自己的集合以支持IBindingList。
我有一个特定数据类(DataItem)的集合类(DataCollection)。该集合实现了接口IBindingList、IList和IList<DataItem>,而DataItem实现了INotifyPropertyChanged(并具有可绑定的公共属性)。
当我尝试通过设置网格的DataSource属性将集合绑定到DataGridView时,如果集合在绑定时不为空,则可以正常工作。否则,如果集合为空,则网格注意到添加或删除行(即DataItems),但单元格保持为空白。与此问题相关的是,在AutoGenerateColumns=true的情况下,网格无法识别数据类的公共成员,因此无法生成列。
我还尝试过使用BindingList<DataItem>来绑定我的DataItems。在这种情况下,即使列表在设置DataSource时为空,网格也能正常工作。另一方面,如果我使用BindingList<object>(但内容相同为DataItems),则其行为与我的DataCollection一样错误。我猜想问题是,如果在绑定时列表为空,数据绑定无法正确检测DataItem类型,并且即使最终向集合中添加项目,它也无法恢复。
重要的是,如果在绑定时集合不为空,则它可以正常工作。
请注意,指定列时会出现相同的错误:
this.dataGridView.ReadOnly = true;
this.dataGridView.AutoGenerateColumns = false;
DataGridViewTextBoxColumn column;
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Id";
column.HeaderText = "Id";
this.dataGridView.Columns.Add(column);
column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "UserName";
column.HeaderText = "UserName";
this.dataGridView.Columns.Add(column);
this.dataGridView.DataSource = myList;
我尝试在我的IBindingList的AllowNew上返回true,但没有观察到任何影响。以下内容也失败了:
var bindingSource = new BindingSource();
bindingSource.DataSource = myList;
this.dataGridView.DataSource = bindingSource;
问题是,我该如何告诉绑定机制识别我的?(谢谢)
更新1:
我创建了一个小测试项目来展示这个问题:
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
class DataItem: INotifyPropertyChanged {
private int _id;
public int Id {
get {
return _id;
}
set {
if (value != _id) {
_id = value;
OnPropertyChanged("Id");
}
}
}
private string _userName;
public string UserName {
get {
return _userName;
}
set {
if (value != _userName) {
_userName = value;
OnPropertyChanged("UserName");
}
}
}
private void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
/// Make a list of type DataItem or object...
//BindingList<object> list = new BindingList<object>() {
BindingList<DataItem> list = new BindingList<DataItem>() {
//new DataItem() {
// Id = 1,
// UserName = "testuser"
//}
};
private void Form1_Load(object sender, EventArgs e) {
DataGridView dataGridView = new System.Windows.Forms.DataGridView();
dataGridView.Size = new Size(this.Width-20, this.Height-30);
dataGridView.AutoGenerateColumns = true;
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Id";
column.HeaderText = "Id";
dataGridView.Columns.Add(column);
this.Controls.Add(dataGridView);
dataGridView.DataSource = list;
list.Add(
new DataItem() {
Id = 3,
UserName = "admin"
}
);
// Make some modifications on the data...
(new System.Threading.Thread( state => {
System.Threading.Thread.CurrentThread.IsBackground = true;
System.Threading.Thread.Sleep(2000);
this.Invoke( (Action)( () => {
list.Add(new DataItem() {
Id = 2,
UserName = "guest"
});
} ) );
System.Threading.Thread.Sleep(2000);
this.Invoke( (Action)( () => {
DataItem user = (list.First( obj => ((DataItem)obj).Id == 3 )) as DataItem;
user.UserName = "Administrator";
} ) );
})).Start();
}
}
如果列表的类型是BindingList<DataItem>
,则它可以正常工作。如果类型是BindingList<object>
,则只有在初始化DataSource
时列表不为空时才能正常工作。