5

I want to set multiple selection in a ListBox using c#.

For example, I have a list of values I want to set these values as selected in the ListBox.

How can I do this?

2 Answers 2

11
MyListBox.SelectedItems.Add(item1);
MyListBox.SelectedItems.Add(item2);
.....
Sign up to request clarification or add additional context in comments.

1 Comment

What if SelectedItems is null? Is it possible that SelectedItems is null?
7

You did not explain much, hopefully your are doing this the WPF way...

Create an IsSelected property on your data items then give the style to your ListBoxItems that selects them whenever the IsSelected is enabled:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
    </Style>
</ListBox.ItemContainerStyle>

Then change the property on your data items, and raise the OnPropertyChanged event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.