Will Gill
Will Gill

Reputation: 587

WP7 - set ListBoxItem.IsSelected to true from c#

I have a ListBox with multiple ListBoxItems. I want to set some of these to 'selected'. I am trying the following but it does not work:

ListBoxItem1.IsSelected = true;

The result is that it compiles with no error, but the list item remains unselected.

Any ideas? many thanks! will.

Upvotes: 0

Views: 305

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124632

So I can't attest to why that happens and if it is a WP7 thing as the docs clearly state:

To select a ListBoxItem in a ListBox, set this property to true.

However, you can always use the ListBox.SelectedItems property:

// to add
listBox.SelectedItems.Add( someItem );

// to remove
listBox.SelectedItems.Remove( someItem );

// to clear
listBox.SelectedItems.Clear();

If multi-select is not enabled you can simply use the SelectedItem and/or the SelectedIndex properties.

Upvotes: 3

Related Questions