This is a migrated thread and some comments may be shown as answers.

RadCheckedListBox check state is not updated with datasource

2 Answers 425 Views
CheckedListBox
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 18 May 2016, 02:55 AM

I have a RadCheckedListBox bound to a list of objects. The RadCheckedListBox's CheckedMember value corresponds to the "IsChecked" property on each object.

Updating that property value on the objects does not update the view on the RadCheckedListBox until the RadCheckedListBox receives focus. Surely this not by design?

Windows 7 Ultimate SP1

.NET 4.5

UI for WinForms Q2 2016

2016.2.503.40

 

C# code example:

 

// This code is a simple WinForms project that has a RadCheckedListBox and a regular WinForms CheckBox.

// When the checkbox is checked/unchecked, the "IsChecked" property of the first item in the list (not the control) is toggled.

// Because of the data binding, the first item in the RadCheckedListBox control should be visually updated to reflect the updated state.

public class SimpleObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}

private IEnumerable<SimpleObject> CreateSimpleObjects()
{
    List<SimpleObject> data = new List<SimpleObject>()
        {
            new SimpleObject() { Id = 1, Name = "Item1", IsChecked = false },
            new SimpleObject() { Id = 2, Name = "Item2", IsChecked = true },
            new SimpleObject() { Id = 3, Name = "Item3", IsChecked = true },
            new SimpleObject() { Id = 4, Name = "Item4", IsChecked = false },
            new SimpleObject() { Id = 5, Name = "Item5", IsChecked = false },
            new SimpleObject() { Id = 6, Name = "Item6", IsChecked = true }
        };
    return data;
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    var firstItem = this.radCheckedListBox1.Items[0].DataBoundItem as SimpleObject;
    firstItem.IsChecked = !firstItem.IsChecked;

    // // This makes it work as expected.
    // var activeControl = this.ActiveControl;
    // this.ActiveControl = this.radCheckedListBox1;
    // this.ActiveControl = activeControl;
}

2 Answers, 1 is accepted

Sort by
0
Ben
Top achievements
Rank 1
answered on 18 May 2016, 12:15 PM

Looks like I omitted the initialization part of the code although it is just boilerplate stuff.

Just in case, here it is:

this.radCheckedListBox1.DataSource = this.CreateSimpleObjects();
this.radCheckedListBox1.DisplayMember = "Name";
this.radCheckedListBox1.ValueMember = "Id";
this.radCheckedListBox1.CheckedMember = "CheckState";

 

FYI, it's almost the same code as the Telerik sample code for Data Binding:
http://docs.telerik.com/devtools/winforms/checkedlistbox/data-biniding    <-- typo lol!

 

 

0
Hristo
Telerik team
answered on 19 May 2016, 10:07 AM
Hello Ben,

Thank you for writing.

The experienced behavior is expected since you are using a List collection and your model object does not implement the INotifyPropertyChanged interface. The List collection will not listen for changes in the properties of your object and hence it will not raise a ListChanged event.

In this respect, RadCheckedListBox will not update when you modify externally the IsChecked property of any of your models. When you move again the focus to the control it updates by synchronizing its visual and data items. Additional information is available here:
In order to achieve your task you have two alternative options:
  1. Stay with the List collection, and force an update in the list box when you change any of your model`s properties:
    this.radCheckedListBox1.ListViewElement.SynchronizeVisualItems();
  2. Use a BindingList, implement the INotifyPropertyChanged in your class and raise the event for any properties which you would like to be notified:
    public class SimpleObject : INotifyPropertyChanged
    {
        private bool isChecked;
     
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public event PropertyChangedEventHandler PropertyChanged;
     
        public bool IsChecked
        {
            get
            {
                return this.isChecked;
            }
            set
            {
                this.isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }
     
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    private BindingList<SimpleObject> CreateSimpleObjects()
    {
        BindingList<SimpleObject> data = new BindingList<SimpleObject>()
        {
            new SimpleObject() { Id = 1, Name = "Item1", IsChecked = false },
            new SimpleObject() { Id = 2, Name = "Item2", IsChecked = true },
            new SimpleObject() { Id = 3, Name = "Item3", IsChecked = true },
            new SimpleObject() { Id = 4, Name = "Item4", IsChecked = false },
            new SimpleObject() { Id = 5, Name = "Item5", IsChecked = false },
            new SimpleObject() { Id = 6, Name = "Item6", IsChecked = true }
        };
     
        return data;
    }
     
I hope this information was useful. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
CheckedListBox
Asked by
Ben
Top achievements
Rank 1
Answers by
Ben
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or