开发者

Select Multiple items in listbox in wpf using c#

开发者 https://www.devze.com 2023-03-16 20:40 出处:网络
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.

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

精彩评论

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