首页 文章

在WinForm应用程序中将List绑定到Repeater

提问于
浏览
0

我最近started this question . 建议的方法是使用 DataRepeater .

我已经看过很多关于如何绑定转发器的例子,但它们都是针对ASP的,而不是Windows Form应用程序 .

我已将 LabelPictureBoxButton 组件添加到模板中,但我无法将 IList<SomeObject> 成功绑定到我的 DataRepeater .

我想用列表中的信息填充这些组件 .

如何在WinForm应用程序中将 IList<SomeObject> 绑定到 DatarRepeater

1 回答

  • 1

    终于搞定了!为了将来参考,这是我使用的:

    首先使用 BindingSource 调用此方法来初始化手动绑定:

    private BindingSource bindingSource ;
    private void InitUserListArea()
    {
        _bindingSource = new BindingSource();
        _bindingSource.DataSource = tempUsers;
        _labelUserListRoleValue.DataBindings.Add("Text", _bindingSource, "Role");
        _labelUserListNameValue.DataBindings.Add("Text", _bindingSource, "Name");
        _labelUserListLastAccessValue.DataBindings.Add("Text", _bindingSource, "LastAccess");
        _dataRepeaterUserList.DataSource = _bindingSource;
    }
    

    然后获取数据(在我的情况下来自web服务)并用数据填充列表 . 填充列表后,或发生任何更改时:

    private void RefreshDataRepeater()
    {
        if (_dataRepeaterUserList.InvokeRequired)
        {
            _dataRepeaterUserList.Invoke((Action)(() => { RefreshDataRepeater(); }));
            return;
        }
    
        _bindingSource.DataSource = null;
        _bindingSource.DataSource = tempUsers;
        _dataRepeaterUserList.DataSource = null;
        _dataRepeaterUserList.DataSource = _bindingSource;
        _dataRepeaterUserList.Refresh();
    }
    

相关问题