使用c#在wpf中的列表框中选择多个项目

kar*_*tal 5 c# wpf select listbox

我想在ListBox使用c#中设置多个选择.

例如,我有一个值列表,我想将这些值设置为选中ListBox.

我怎样才能做到这一点?

Nic*_*k B 11

MyListBox.SelectedItems.Add(item1);
MyListBox.SelectedItems.Add(item2);
.....
Run Code Online (Sandbox Code Playgroud)


Mar*_*mić 6

你没有解释太多,希望你这样做的WPF方式......

IsSelected在您的数据项上创建一个属性,然后为您的ListBoxItems 提供样式,以便在IsSelected启用时选择它们:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
    </Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

然后更改数据项的属性,并引发OnPropertyChanged事件.