为什么 checklistbox 没有数据源?如何绑定到值列表?
我正在开发一个 Winform,我需要一个复选框。我将值存储在具有 List 属性的对象中:
public static class Fields
{
public static IList<string> FieldList { get; set; }
static Fields()
{ ...//populate FieldList }
}
现在我希望我的 CheckedListBox 使用 Fields.FieldList 作为数据源。在线搜索后,我发现我需要设置
//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;
But myCheckedListBox 没有 DataSource 属性。
我在这里错过了什么吗?
I am developing a Winform and I need a checkedlistbox. I have the values stored in an object which has a List property:
public static class Fields
{
public static IList<string> FieldList { get; set; }
static Fields()
{ ...//populate FieldList }
}
Now I would like my CheckedListBox to use Fields.FieldList as datasource. After searching online I found I needed to set
//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;
But myCheckedListBox does not have a DataSource property.
Am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据文档,它应该具有此属性... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx
但是,我也有同样的不久前在一个项目上遇到问题,并使用这篇 CodeProject 文章 编写了解决方案在我需要此功能的一个项目中。
研究了一下,我确实发现了这个:
http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden
编辑:上面的链接不再有效,但下面的摘录来自曾经驻留在那里的文章。
这解释了为什么该属性存在,但没有在 Intellisense 中显示。
这篇博文也值得一读: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/
Per the documentation, it should have this property... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx
However, I also had the same issue on a project a while back, and used this CodeProject article to code the solution in the one project where I needed this feature.
Researching a bit more, I did find this:
http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden
Edit: The above link is no longer working, but the exceprt below is from the article that once resided there.
That explains why the property exists, but doesn't show in Intellisense.
This blog post is worth a read as well: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/
以下是我将
User
对象的List
绑定到 CheckedListBox 的方法。当然不建议这样做,因为文档告诉我们该属性是隐藏的。
上面的代码可以工作,但我注意到 Visual Studio 2012 中存在一些副作用,例如:
渲染选中标记的延迟:
单击所需的项目后,渲染“选中”标记会出现烦人的延迟。
就我而言,
CheckOnClick
属性为 True,CausesValidation
为 False。Here is how I am binding a
List<T>
ofUser
objects into CheckedListBox.Of course this is not recommended, since documentation is telling us that this property is hidden.
The code above works, but I noticed some side effects in Visual Studio 2012 such as:
Delay for rendering checked marker:
After you click on the desired item, there is annoying delay to render the "checked" marker.
In my case,
CheckOnClick
property is True,CausesValidation
is False.就我个人而言,我使用绑定到
DataTable
的DataGridView
,该 DataTable 具有布尔字段以及显示值字段。如果您隐藏列标题和行标题,那么您将获得与
CheckedListBox
所提供的非常接近的内容。Personally I use a
DataGridView
that is bound to aDataTable
that has a Boolean field along with a field for the display value.If you hide the column headers and row headers then you get something pretty close to what a
CheckedListBox
gives you.可以通过迭代您的潜在数据源并一次添加其项目来解决此问题。例如:
这样会引发异常:
可以修改为这样:
This can be worked around by iterating through your would-be datasource and adding its items one-at-a-time. For example:
This, which will cause an exception:
Can be modified to this:
我通过将 ToString () 方法更改为应该出现的名称解决了这个问题:
并变成带有对象的数组列表:
I solved the problem by changing the ToString () method to the name that should appear:
and turning into array list with objects:
我知道这已经很老了;为了任何仍然有相同要求的人的利益,这对我有用。请注意,我没有使用
DisplayMember
或ValueMember
属性,因为它似乎是不鼓励的(请参阅上面@David Stratton 的帖子)。I know this is pretty old; for the benefit of anyone who still has the same requirement, here's what worked for me. Note that I did not use
DisplayMember
orValueMember
properties, as it seems like it is discouraged (see @David Stratton's post above).