WinForms中使用IBindingList进行数据绑定时,空列表会导致失败。

3

我有一个问题,即实现自己的集合以支持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时列表不为空时才能正常工作。

1个回答

7

数据绑定将从查找列表项开始,尝试获取它们的属性,但是对于空列表,它将从列表项的Type中获取所有信息。如果使用空的BindingList<object>,数据绑定无法发现任何属性的原因是object没有可绑定的属性。

为了完全确保您的DataCollection类在为空时也能正确支持绑定,请实现ITypedList接口。它包括方法GetItemProperties(),该方法允许您明确指定哪些属性是可绑定的。在此方法中,您可以使用以下内容返回DataItem上的属性:

return TypeDescriptor.GetProperties(typeof(DataItem));

即使集合为空,数据绑定也会知道要显示哪些属性。


解决方案可行且非常简单,我只是在搜索互联网时找不到它。非常感谢!! - thaller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接