Select Multiple items in listbox in wpf using c#
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 开发者_JAVA百科as selected in the ListBox
.
How can I do this?
MyListBox.SelectedItems.Add(item1);
MyListBox.SelectedItems.Add(item2);
.....
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 ListBoxItem
s 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.
精彩评论