Set item in collection to Checked from Code behind

Mar 17, 2011 at 5:07 PM

Is there a way that I can set certains items in the collection to be checked from the code behind? 

Coordinator
Mar 26, 2011 at 2:46 PM

Hello,

Sorry for the delay!

As the control inherits from the regular ListBox, you should be able to set the selected items.

To do so, first set the ListBoxWithCheckBox in Choose State, and then, you can use the SetSelected method.

// ChooseState
this.listBoxWithCheckBoxes.IsInChooseState = true;

int index = /* Put here the item index you want to select */;
this.listBoxWithCheckBoxes.SetSelected(index, true);

You can also use a loop on the Items collection and set each item to be selected:

// ChooseState
this.listBoxWithCheckBoxes.IsInChooseState = true;

foreach(ListBoxItem aListBoxItem in this.listBoxWithCheckBoxes.Items) {
   if (/* Put the code to see if you want to select it */) {
      aListBoxItem.IsSelected = true;
   }
}

Best regards,
Olivier
Jan 29, 2012 at 7:25 AM

I couldn't find SetSelected() method. Is it discontinued or am I missing anything?

Oct 30, 2014 at 4:23 AM
You can define whether an item is checked or not programmatically simply by following this listbox library guide.