C# 在WinForm应用程序中将列表绑定到中继器
我最近很紧张。建议的方法是使用C# 在WinForm应用程序中将列表绑定到中继器,c#,winforms,list,binding,repeater,C#,Winforms,List,Binding,Repeater,我最近很紧张。建议的方法是使用数据转发器 我看过很多关于如何绑定repeater的示例,但都是针对ASP的,而不是针对Windows窗体应用程序的 我已将标签、图片盒和按钮组件添加到模板中,但我无法成功地将我的IList绑定到我的数据转发器 我想用列表中的信息填充这些组件 如何在WinForm应用程序中将IList绑定到DatarRepeater?终于让它工作了!为了将来参考,我使用了以下内容: 首先调用此方法来初始化手动绑定,使用BindingSource: private BindingSo
数据转发器
我看过很多关于如何绑定repeater的示例,但都是针对ASP的,而不是针对Windows窗体应用程序的
我已将标签
、图片盒
和按钮
组件添加到模板中,但我无法成功地将我的IList
绑定到我的数据转发器
我想用列表中的信息填充这些组件
如何在WinForm应用程序中将
IList
绑定到DatarRepeater
?终于让它工作了!为了将来参考,我使用了以下内容:
首先调用此方法来初始化手动绑定,使用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();
}
终于成功了!为了将来参考,我使用了以下内容: 首先调用此方法来初始化手动绑定,使用
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();
}
someObject中是否只有字符串?如果是这样,您可以像下面这样绑定列表:DataRepeater.DataSource=list.Select(x=>new{Value=x}).ToList();不,还有自定义类型。我希望绑定一些字段,而不是所有字段。对于iInstance,
var obj=new SomeObject()
obj.Name
转到标签,obj.Image
转到PictureBox等。您知道这是怎么回事吗?某个对象中是否只有字符串?如果是这样,您可以像下面这样绑定列表:DataRepeater.DataSource=list.Select(x=>new{Value=x}).ToList();不,还有自定义类型。我希望绑定一些字段,而不是所有字段。对于iInstance,var obj=new SomeObject()
obj.Name
转到标签,obj.Image
转到PictureBox等。您对如何实现这一点有何想法?