Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. I know how to databind to a source and how to programatically change the entries by iterating through the 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.

like image 430
FailedDev Avatar asked Sep 20 '11 13:09

FailedDev


2 Answers

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<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

like image 130
Rami Alshareef Avatar answered Oct 19 '22 11:10

Rami Alshareef


You can find answer here: Using datasource with CheckBoxList

var checkBoxList = (ListBox)MyCheckBoxList;
checkBoxList.DataSource = dataSource;
checkBoxList.DisplayMember = "name";
checkBoxList.ValueMember = "enabled";

Make sure that the ValueMember is of type bool.

like image 43
Samich Avatar answered Oct 19 '22 09:10

Samich