How do I get the list of selected items from a ListCollectionView? I see CurrentItem returns for single selection but I don't see anything to return the list of selected items in multi-select mode.
ListCollectionView or other types implementing ICollectionView are wrapper over source collection which WPF creates internally to bind with instead of direct binding to source collection. So, it does not have any such property to get selected items on GUI which is more or less a UI thing.
You need to get the SelectedItems from an UI component. ListCollectionView must be binded to ListBox. Access ListBox SelectedItems property to get selected items on view.
Related
I have a list box object and it contains some items. I want to move selected item of the list box to the top of the same listbox
Bind your List Box ItemsSource to ICollectionView instead of directly to an ObservableCollection and that will allow you to easily sort, filter or group your underlying collection without making any changes to the collection itself.
Check out this tutorial at wpftutorial.net:
How to Navigate, Group, Sort and Filter Data in WPF
My View (V) is bound to a ViewModel (VM). In the V I have a List of objects that is displayed in a ListView. In the ListView I use an ItemTemplate, each item is displayed as a Label holding the name of the item and a Combobox with the SelectedItem set to another property of the item. The Combobox is bound to a List which is also part of the VM of the main V (I bind it using ElementName=ListBox.DataContext in the Binding...)
So far everything works very well. The problem is that the Combobox can contain a lot of entries and I would like to use grouping or filtering. I know how to use filtering and grouping using ListCollectionView. But I cannot simply change my List to a ListCollectionView since it is used multiple times (for each item in the ListView). One ListCollectionView cannot be used multiple times but have different SelectedItem for each usage.
Now I am looking for an easy way to use grouping in the ListView's Combobox.
Is there a way to define the ListCollectionView directly in XAML inside the ItemTemplate (DataTemplate)? I also thought about using a (non-shared) Converter in the ItemsSource Binding that converts my List of objects into a ListCollectionView (if I use OneTime Binding, the Converter will only be called once, is that correct?)? I know I could also put each object in the List in the VM in its own ViewModel and each of it provides its own ListCollectionView for the Combobox, but that seems to be the most complicated solution (but maybe the cleanest?)
Any other suggestions or advice?
If you want to use the same ICollectionView in multiple comboboxes (or other selectors) without synchronizing their selected item, you need to explicitely set IsSynchronizedWithCurrentItem="False" on each combobox.
I set up an ItemsControl with the items source set to a List. I then filter this ItemsControl using code like the following:
CollectionViewSource.GetDefaultView(myList).Filter = filterFunction;
This worked as expected. However, I have since changed the items source to a ReadOnlyCollection. The items still show up in my ItemsControl, but the filter is no longer applied.
Changing the Items source back to a List restores the filter functionality. Is this expected behavior? How can I use a filter with a ReadOnlyCollection as the items source?
how can i bind SelectedItems of a ListView?
My ListView has multipleSelection attribute and I'm using CollectionView for its contents..
I've heard about Attached property and I tried implementing this with the one I found here:
Sync SelectedItems in a muliselect listbox with a collection in ViewModel
I can multiple select the items by clicking rows but I can't use the Shift keyboard for multi-selecting many rows instantly... Also, when I filter my collection and refresh it, my selection are all deselected after the refresh..
How can I make it so that whenever my CollectionView refreshes, the previously selecteditems are still selected after the refresh...?
Can someone also help me how to manipulate logically the selected items through my viewmodel?
May be you should add the IsSelected property to the ListViewItem's view model.
You will have to use your own code to keep the selected items after a refresh. Maybe make a copy of your collection before the refresh and afterwards a simple for to check all the checked items in your current collection.
Change your selectection mode to extended for your listbox for the shift key to work.
As for manipulating logically the selected items, you will have to give a lot more info on what exactly you want done.
I'm using an observable collection to hold all files dragged into a ListBox control and binding the collection to the itemssource, now I am using an extended selection mode so I can select more than one item in the ListBox, my problem is if I have selected index 0, 4 and 7 as an example, how could I bring these values up into an array?
As there is no way to bind to the SelectedItems property of the ListBox control, you will need to watch for this in the back-end (either ViewModel or code-behind).
Depending on when you want this to happen would depend on your approach.
If you want the indexes to be updated OnSelectionChanged you will need to hook that event either using an event handler, or using the AttachedCommandBehaviour approach.
To get the indexes (collection index, not necessarily display index) you will then loop through the SelectedItems collection and get the IndexOf value from the ItemsSource collection (in your case the ObservableCollection).
If your list has been sorted after setting the ItemsSource you may need to take a different approach, however.