【问题标题】:Binding a List to Repeater in WinForm application在 WinForm 应用程序中将列表绑定到中继器
【发布时间】:2013-03-12 19:57:21
【问题描述】:

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

我看过很多关于如何绑定转发器的示例,但都是针对 ASP,而不是 Windows 窗体应用程序。

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

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

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

【问题讨论】:

  • someObject 中是否只有字符串?如果你可以像这样绑定你的列表 DataRepeater.DataSource = list.Select(x=>new {Value = x}).ToList();
  • 不,还有自定义类型。我希望绑定一些字段,而不是全部。例如,var obj = new SomeObject(); obj.Name 转到标签,obj.Image 转到图片框等。关于这怎么可能的任何想法?

标签: c# winforms list binding repeater


【解决方案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;
}

然后获取数据(在我的例子中来自网络服务)并用数据填充列表。填充列表后,或发生任何更改时:

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2017-11-03
    • 2011-04-01
    • 2019-02-26
    相关资源
    最近更新 更多