简体   繁体   中英

WPF ListBox, when selecting several items, why is the first item in the multi item selection omitted?

Hi I have a ListBox and set it up as follows in xaml:

<dxdo:LayoutPanel Caption="Raw Data File Names" ItemWidth="2*">
                    <ListBox ItemsSource="{Binding FilteredFileNames}" SelectionMode="Extended" SelectionChanged="Selector_OnSelectionChanged"/>
                </dxdo:LayoutPanel>

When I handle the event in code-behind each time I select multiple items (via shift-down plus mouse click) I noticed that the first item is never included in the array of items:

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var vm = DataContext as HistoricalDataImportRawDataViewModel;
        vm.SelectedFileNames = e.AddedItems.Cast<string>().ToList();
    }

What am I doing wrong? Is it because AddedItems only include the items beyond the initial selection? What can I do to get the complete collection of items? Please note that I have to use SelectionMode="Extended". Is the omission of the first item intended or a bug?

The AddedItems property tells which item was added to the selected items. If you are interested about all the items that are selected, you have to access ListBox property SelectedItems .

    var listbox = (ListBox) sender;
    var selectedItems = listbox.SelectedItems
         .Cast<string>()
         .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM