Move an item in a listbox WPF - c#

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

Related

Accommodate custom item on winrt listview

I'm developing windows metro app. In my application, I've one Listview with wrapgrid in itemspanel to display list of items on vertical rows with specific height. I want to display one item on top of the first column of list view, which shows result/stats of list items.
I would like to know if it is possible without adding custom item to datasource of listview?
ListView has a Header property in which you can Place content before the ListViews Items.
As commented, I've implemented following solution which is not elegant but worked for me.
Use Datasource converter to add dummy item in main list. So, my original list remains as is.
Use Template Selector to bind different template for first item.
Handle Selection and clicked event of dummy item.

How to retrieve list of multi selected items from ListCollectionView

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.

Populating a panel with selected items from a grid in WPF

I have a DataGrid with a bunch of rows representing items in my system. I want that each time a user selects an item in the grid (the user is allowed to select multiple items) the item will be added to a StackPanel and have its own datatemplate using an Expander to display its data.
Can anyone help me do this in WPF?
Thanks!
John.
I wouldn't use a StackPanel, but an ItemsControl, and bind its ItemsSource to the DataGrid's SelectedItems. However, at the moment I'm not sure whether SelectedItems has change notification. If not, you might have to use a CollectionViewSource in between, and call CollectionViewSource.Refresh during the DataGrid.SelectionChanged event.

Binding SelectedItems of Listview

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.

How to get index value of Extended Selection Mode WPF ListBox?

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.

Categories