CheckedListBox 并将对象属性绑定到项目
我很好奇是否有一种简单的方法将类的属性绑定到 CheckedListBox 的各个项目。有几个组件(Telerik/DevExpress)提供 PropertyEditor 网格,但我希望在 CheckedListBox 中实现。
IE:
public class MyItem
{
public bool Property1
{
get;
set;
}
public bool Property2
{
get;
set;
}
public bool Property3
{
get;
set;
}
}
当向 CheckedListBox 添加项目时,我希望有某种方法可以让我这样做:
this.AddCheckListBoxItem("Property A", this.myItem.Property1);
this.AddCheckListBoxItem("Property B", this.myItem.Property2);
this.AddCheckListBoxItem("Property C", this.myItem.Property3);
第一个参数是 CheckedListBox 中的显示名称。
然后,在检查状态发生任何更改时,布尔值都会自动更新,无需进一步的代码。
I'm curious if there is a simple way to bind properties of a class to individual items of a CheckedListBox. There are several components out there (Telerik/DevExpress) that provide PropertyEditor grids, but I'm looking to do it in a CheckedListBox.
IE:
public class MyItem
{
public bool Property1
{
get;
set;
}
public bool Property2
{
get;
set;
}
public bool Property3
{
get;
set;
}
}
And when adding items to the CheckedListBox, I'd like to have some sort of method that lets me do:
this.AddCheckListBoxItem("Property A", this.myItem.Property1);
this.AddCheckListBoxItem("Property B", this.myItem.Property2);
this.AddCheckListBoxItem("Property C", this.myItem.Property3);
the first parameter being the display name within the CheckedListBox.
Then throughout any changes to the checkstate, the bool values would automatically be updated without further code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前没有任何简单/简单的方法来获得您正在寻找的功能。
正如评论中一样,我能想到的最接近的解决方案是使用反射。
如果您设法构建一个具有此功能的帮助器类,请在此处发布,因为我也发现这很有用。
Currently there isn't any easy/simple way to get the functionallity you are looking for.
As in the comments the nearest solution I can think of would be to use reflection.
If you manage to build a helper class that has this functionallity, please post here as I would also find that usefull.
我已经按照 @Jethro 的建议使用反射创建了一个解决方案。我在类定义中使用了泛型,但我还没有实现任何使用它们的东西。总而言之,基本上使用反射,我将单个对象及其布尔属性绑定到标准 CheckedListBox 中的各个项目。这很好,因为它不需要编写代码来设置属性值并在保存数据时获取它们,因为反射会处理这个问题。
我创建了一个表单,并向其中添加了一个 CheckedListBox 和一个按钮。这是代码方面。
I've created a solution using reflection as suggested by @Jethro. I used generics in the class definition, but I didn't implement anything that uses them yet. To summarize, basically using reflection, I've bound a single object and it's boolean properties to individual items within a standard CheckedListBox. It's nice as it doesn't require having to code to set the property values and get them when saving data, as the reflection takes care of this.
I created a Form, and added a CheckedListBox to it, and a button. Here's the code side.