Populating a panel with selected items from a grid in WPF - c#

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.

Related

ItemsControl within WrapPanel vs ListBox within WrapPanel

Background: I have a WrapPanel with Vertical Orientation that I want to put up to 40 "items" in. Each "item" within the WrapPanel will contain a combination of buttons and expandable lists in a 5x6 grid format. I need to be able to select each "item" within the WrapPanel so it brings up a dialogue box where the user can click the buttons and edit the expandable lists for that "item".
Question: I understand that the main difference between ItemsControl and ListBox is that the entities inside a ListBox are selectable while the ones in ItemsControl are not, however I do not need to select values inside my ItemsControl or ListBox until my dialogue appears, so I'm not sure if it would be best to use the ItemsControl or ListBox in this situation. Which ListControl would be best for my situation?
If your root items should be selectable you would probably want a ListBox whose ItemsPanel is a WrapPanel. If you don't need actual selection (i.e. it does not matter that items automatically deselect, or that operations are executed on the set of selected items) you can just use an ItemsControl with a WrapPanel as ItemsPanel and use a big Button as the root of your ItemTemplate (to trigger the dialog).

Virtualize WPF Grid bound to composite collection

I have a Listview that uses a grid for its ItemsPanel instead of a stackpanel (a stackpanel won't suffice).
The ItemsSource of a grid is bound to a CompositeCollection of various items; and the items, of different classes, are not ordered (i.e., first item is not necessarily in 1st row/ 1st column; it's possible that last item in the list may physically be found on the top row of the grid).
So now I am not able to use VirtualizingStackPanel,... when I check Snoop, the ListView lists all items in the ItemsSource even when they're not visible.
Question: Is there a way to virtualize contents inside of the grid?

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.

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.

Winform ListView databind

I have a Listview that uses databind. I set the DataSource property to a binding source. All works fine. The problem is that I need to have a column that is not databinded and contains only buttons that have the same handler for click event. To accomplish this I tried to add a subitem that is a button for each ListViewItem after InitializeComponent but doesn't work, nothing is displayed. Also I set the list view column type to Control.
If I add elements to ListView and isn't databinded that the buttons appear.
So it will be a great help for me to know if buttons could be displayed in column that is not databinded when the listview uses databinding for rest of columns.
Thanks!
The best thing to do here (assuming you mean ListBox), is to have a single button above or below the listbox, that uses the ListBox.SelectedItem property to investigate the selected item and do something with it.

Categories