Winforms : How to bind the Checkbox item of a CheckedListBox with databinding

前端 未结 3 2099
旧时难觅i
旧时难觅i 2020-12-03 17:17

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

3条回答
  •  孤街浪徒
    2020-12-03 17:56

    According to Samich's answer, Here is a full example, the binding source is an Object

    private void Form1_Load(object sender, EventArgs e)
            {
                List lst = new List();
    
                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

提交回复
热议问题