Removing Listview Jumping - c#

I have a ListView which I am constantly adding items in.
I am able to scroll while the items in the ListView were being added.
However, upon selecting an item in the ListView, I am unable to scroll while items are being added. For each ListView.Items.Add(Object) I call, I will revert to the focus of the item initially selected.
Clearing the SelectedItems and SelectedIndices, I am still able to continue scrolling down the ListView as I keep popping back to the originally selected item anytime an item is added.
Could anyone advise how to get past this problem?
Thank you.
Regards,
Michael

If you're using WPF, are you using an ObservableCollection? It sounds like the DataContext is being changed causing the ListView to reload. If you bind to an ObservableCollection you may have more luck.

Related

ListView scroll to bottom as all binding data loaded

I have a ListView in my xaml which is binded to ReadOnlyObservableCollection which contains items.
How can I scroll to the bottom of the ListView when all the binded list loaded?
I've tried to use the myList.ScrollToBottom() function as I add the data to mylist (on ViewModel) - the scroll isn't visible, it doesn't scroll till the end of the list.
I've tried to subscribe to Lists Loaded , CollectionChanged and ItemContainerGenerator.ItemsChanged events, and then to scroll to the bottom didn't helped either.
If I subscribe to my event (on ViewModel) which I fire as I set the data to my List, and then I call ScrollToBottom function with a slight delay - the scroll is perfect, so I guess it's timing issue...
Any help will be appreciated.
Try to use this:
myList.ScrollIntoView(myList.Items[myList.Items.Count - 1])
How are you doing this if you have a ViewModel? The ViewModel wouldn't know what control you're talking about. So how can your ViewModel reference "myList"?
I'm also trying to get the ListView to scroll to the last item when the collection is updated, but none of the ScrollTo features are available in in the XAML: THere's no way to bind to them. These seem to all be METHODS, meaning you can't do property MVVM with it.
Or am I missing something?

WPF Adorner and ICollectionView Refresh

I have a list box bound to a collection of items.
Whenever i call a Refresh on a CollectionView of bound items the adorners disappear from list box items.
Can somebody explain why and how i can overcome this?
Should i re-add adorners after each refresh?
An adorner is linked to a particular element, in your case it seems like you are adorning ListBoxItem elements.
When you call the Refresh method on the collection view, this will cause the ListBox control to delete all of its existing ListBoxItem children and recreate them. Your adorners are "disappearing" because the elements they are adorning have been deleted by the list box.
You can either recreate the adorners or try to find some way of refreshing your list box that doesn't involve refreshing the collection view.

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.

ListBox not updating

I am using a ListBox in one of the windows form application. When the screen first initializes, I am able to add/remove item into the listbox. However, after I click on one of the buttons to change the content of the listbox, the listbox does not update the values. I tried putting the Listbox inside a Panel and Refreshing the panel to no avail. Anyone ran into same problem?
It sounds as though the data source you are binding to does not support IBindingList.

Freezing a listboxitem while items are being added

We have a ListBox that has a number of items. Items are inserted into the ListBox via an ObservableCollection. Some of these items can be edited right in the ListBox. However, if an item is added at an index < the edited item's index, the entire content of the ListBox moves down.
What we'd like to do is the following: if an item is in edit mode, we'd like to freeze its position on the screen. It is fine if items are added to the collection and the UI around the item changes. But the position of the item should remain constant on the screen.
The only thing I've been able to do so far is attach to the ScrollChanged event and, at most, use either BringIntoView or ScrollIntoView methods to ensure that the item is always displayed somewhere in the UI, but I am unable to lock down its position.
Has anyone done something like this and help out?
I think the following would solve your problem:
When entering edit mode, keep a reference to the object you're editing, its index in listbox and the ScrollViewer's HorizontalOffset.
In a handler to the ObservableCollection.CollectionChanged event find the new index of the object you editing, if the index changed, swap the edited item with the one now taking it's place (or some other logic if you want to keep a certain order). Then if the ScrollViewer.HorizontalOffset changed, move it back to the said offset.
This will make sure the item you're editing will always stay in the exact same place in the list and in the UI.
Hope this help you out.
You could always force items to be added to the end of your collection. Otherwise, I think you are on the right track with scrolling the ScrollViewer. When entering 'edit mode', track the current horizontal offset.
ScrollViewer sv = listBox.GetScrollViewer();
double indexPos = sv.HorizontalOffset;
However, I think you would be better off attaching to the ObservableCollection.CollectionChanged event. When the event fires, check if the new value was inserted above your 'edit mode' item. If it is, then run the following code.
ScrollViewer sv = listBox.GetScrollViewer();
sv.ScrollToHorizontalOffset(indexPos+1); // This will obviously require an offset
Is the problem that you simply don't want it moving? Or is the problem that you have referenced said item by its index number and the change in index causes other problems when you apply the changes (like applying them to the item that is now at the previous index)?
If the problem is that a change of index causes problems in code, then I would stop using the index and instead get a reference to the object. Later you can always get the index of the item if you find you need it.
Would it be feasible to switch to a ListView, and use EnsureVisible?
If freezing the rest of the listbox is a viable option. Such that items which are added don't appear until after the edit has been completed or escaped then you could try using BeginUpdate and EndUpdate to stop the ListBox from being repainted. I'm not sure how that would effect your editing process though.

Categories