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.
Related
I have a DataGrid I bound to an ObservableCollection. The grid is configured in a way that the user can add rows (this is the desired behavior). Elsewhere in my GUI, I bound the same collection to be the ItemsSource of a ComboBox. There, it shows the {NewItemPlaceholder} I don't want.
I found a similar question where it is suggested to set CanUserAddRows to false but then I would need a dedicated button or context menu item to add a new row which I find less intuitive.
So, is there a way to get rid of this placeholder by e.g. some means of filtering the ItemsSource?
Followup 1
I discovered the following strange behavior:
When I directly bind the ItemsSource of the combo box to the observable collection, I get the {NewItemPlaceholder}
When I declare a CollectionViewSource as a Resource of my combo box, set its Source to the observable collection and set in turn my CollectionViewSource as the ItemsSource of the combo box, the {NewItemPlaceholder} disappears. I don't even have to specify a filter.
I'm having a problem with a DataGrid and its underlying ObservableCollection. I want to add and remove rows programmatically from the collection and have the results appear in the DataGrid. While I expect that this will automagically happen for me, it doesn't. I've found that if I sort the data afterwards, that the rows will appear as I want them. Here's what I do:
DataGrid's ItemsSource is set to my ObservableCollection<T>.
User clicks button that fires an ICommand that removes rows with a value of X.
What the user sees looks exactly the same: no rows are removed.
User clicks column header to sort the DataGrid. Now the rows are removed.
What's going on here? The rows that I want removed, stay until I sort the columns. I found out something else as well. If I refresh the DataGrid.Items, the rows are removed. That code is simply:
this.dg.Items.Refresh(); //"dg" is my DataGrid
While this is not very MVVM, it works.
Make sure you're binding directly to your ObservableCollection, e.g., you are not tacking on any Linq operators, wrapping it in a collection view/proxy, etc. Any transformation/wrapper on your collection that does not forward collection change events will prevent the grid from being notified of changes.
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.
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.
Apologies for not posting any code for this but all my efforts seem to be going nowhere.
I need to get the name of an observableCollection a textblock is bound to from the mouseDown event handler, so I can then perform some operations on the data, is there any way to do this?
I have set the Tag of the textblock to {binding} so i get the entire object back the textblock is bound to. Other than this im not sure where to go next.
Update:
The reason for this is that I have 2 multiselect treeviews, with a heirarchial data template, each template shares the same treeview but is bound to a different observable collection.
The way my multiselect works is by applying a style to each treeview if the IsSelected value of that item in my collection is true.
Now I am using the Mousedown event handler for the textblock in my datatemplate to get the item I am working on, BUT some items can be in both collections at once. I need to know which item to set the IsSelected value on. Using Binding{tag} I get the Item I need to set on but not the collection it is within.
I'm using Mousedown as the event handler because If you click on one item to select it, then click again it needs to unselect and the treeview event handlers didnt seem to allow this to happen (SelectedItemChanged etc).
As a side note I also need to be able to hide the default selected style of the Treeview as this isnt used and it gets confusing.
You can't determine what collection an item is in, but you can determine what TreeView the user clicked on. Then you can get the collection by knowing the TreeView, which should solve your immediate problem.