[英]Winforms : How to bind the Checkbox item of a CheckedListBox with databinding
I have a databinded checkedlistbox in one form and I would like to know if it is even possible to databind the check box of each list box item with a certain property of an object. 我在一个表单中有一个databinded checkedlistbox,我想知道是否甚至可以使用对象的某个属性对每个列表框项的复选框进行数据绑定。
Thanks for any help in advance :) 在此先感谢您的任何帮助:)
Edit : Perhaps my question was misinterpreted. 编辑:也许我的问题被误解了。
I would like to know if it is possible to databind the checkbox for each Item of CheckedListBox. 我想知道是否可以对CheckedListBox的每个项目的复选框进行数据绑定。 I know how to databind to a source and how to programatically change the entries by iterating through the itmes. 我知道如何数据绑定到源以及如何通过迭代itmes以编程方式更改条目。 What I don't know is if it is possible to have a class which implements INotifyPropertyChanged so that when a "CheckedState" property changes the CheckedListBox updates itself. 我不知道的是,是否有可能有一个实现INotifyPropertyChanged的类,以便当“CheckedState”属性更改时CheckedListBox更新自身。
According to Samich's answer, Here is a full example, the binding source is an Object
根据Samich的回答,这是一个完整的例子,绑定源是一个Object
private void Form1_Load(object sender, EventArgs e)
{
List<randomClass> lst = new List<randomClass>();
lst.Add(new randomClass());
lst.Add(new randomClass());
lst.Add(new randomClass());
lst.Add(new randomClass());
lst.Add(new randomClass());
lst.Add(new randomClass());
((ListBox)this.checkedListBox1).DataSource = lst;
((ListBox)this.checkedListBox1).DisplayMember = "Name";
((ListBox)this.checkedListBox1).ValueMember = "IsChecked";
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
randomClass obj = (randomClass)checkedListBox1.Items[i];
checkedListBox1.SetItemChecked(i, obj.IsChecked);
}
}
}
public class randomClass
{
public bool IsChecked { get; set; }
public string Name { get; set; }
public randomClass()
{
this.IsChecked = true;
Name = "name1";
}
}
randomClass
is for demonstration purposes randomClass
用于演示目的
You can find answer here: Using datasource with CheckBoxList 你可以在这里找到答案: 在CheckBoxList中使用数据源
var checkBoxList = (ListBox)MyCheckBoxList;
checkBoxList.DataSource = dataSource;
checkBoxList.DisplayMember = "name";
checkBoxList.ValueMember = "enabled";
Make sure that the ValueMember
is of type bool
. 确保ValueMember
的类型为bool
。
I just got how to databind a table in sql to a checkboxlist without stress. 我刚刚得到了如何将sql中的表数据化为一个没有压力的checkboxlist。 I am more than excited to share it. 我很高兴分享它。 I added them manually... 我手动添加了它们......
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn.ConnectionString = "Data Source=MICMIKE\\SQLEXPRESS;Initial Catalog=Enterprise;Integrated Security=True";
conn.Open();
string query = "Select Position from Position";// position column from position table
cmd.Connection = conn;
cmd.CommandText = query;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string myItem = dr["Position"].ToString();
checkedListBox1.Items.Add(myItem, true);//true means check the items. use false if you don't want to check the items or simply .....Items.Add(myItem);
}
To Access the items checked in the checklistbox, use 要访问核对表列表中选中的项目,请使用
foreach(object item in Checkedlistbox1.CheckedItems)
{
string itemchecked = item.ToString();
MessageBox.Show(itemchecked);// This will show all the checked items in the checklistbox.
}
It is really working. 它确实有效。 I just got it now. 我现在就明白了。 I hope you like it! 我希望你喜欢它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.