Select multiple items in a list in wpf using c #

I want to set multiple choice in ListBoxusing C #.

For example, I have a list of values โ€‹โ€‹that I want to set as values โ€‹โ€‹in ListBox.

How can i do this?

+5
source share
2 answers
MyListBox.SelectedItems.Add(item1);
MyListBox.SelectedItems.Add(item2);
.....
+9
source

You have explained a little, I hope you do it in a WPF way ...

Create a property IsSelectedin your data elements, then specify a style for yours ListBoxItemthat will select them when enabled IsSelected:

<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 event OnPropertyChanged.

+5
source

All Articles