开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜