Freezing a listboxitem while items are being added - c#

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.

Related

How to get ReorderThemeTransition in ListView?

I have an ObservableCollection where I move the items by
oc.Move(oldIndex, newIndex);
This ObservableCollection is used as ItemsSource for a ListView. Now I want the ReorderThemeTransition or RepositionThemeTransition, but unfortunately there seems to be a full reset. If I add or delete items, everything is fine (a scaling animation, like it should).
I checked the CollectionChanged Action, but there I get only Moves (as supposed to, no Reset or anything) and I also tried to Move only one element at a time, but still the full list flashes and is reordered.
I tried to add ReorderThemeTransition (and all other transitions) to ListView.Transitions, ListView.ItemContainerTransitions and to the ItemsPanel, which is a ItemsWrapGrid as ItemsWrapGrid.ChildrenTransition.
Any ideas, what else I could try?

How to update ListBox control with Dictionary binded to it without jumping to the first item

Is there any way to update a ListBox control with Dictionary binded to it without resetting its DataSource property on every dictionary change? The reason I don't like this solution is that it forces a ListBox control to jump to the first item as mentioned in other questions like this.
Here is a minimal example that reproduces this behavior -- https://bitbucket.org/ntrophimov/updating_issue (about 20 lines of code to read in the MainForm.cs file)
Is there any other solution for this problem?
Is there any dictionary implementation in which I can manipulate items (add and remove them) and these changes will be immediately represented in a ListBox control without refreshing its whole content?
The idea is:
You show your ListBox with items
User click on some item
Applications save the 'selected' item in local variable
User/App initiate ListBox update process
Update process done -> application restore (re-assign) the 'selected' item (if it still valid for current items of ListBox)

Scroll to item in view, but do not scroll to view

I'm developing a WPF application, containing multiple ListView's.
Every view has a button, and the user can press it to initiate a function that processes the items in the view.
The item count is often up in thousands, and can take a couple of minutes to process.
Now, I want the item currently being processed to be selected and scrolled to so the user can get a hunch of how much has been processed.
This in turn also makes it easy for the user to know what item causes the processing to stop in the event of an error, as it is selected.
To do this I have bound the ListView's SelectedIndex to my ScrollRow property, which i set in my processing function.
I then scroll to the selected item using the event SelectionChanged, which runs a function that calls ScrollToView on the selected item.
My window contains many views, so there is often a scrollbar on the main window containing the views.
The problem is that when I call ScrollToView on a row in a view, the main window also scrolls to this view, when the user wants to look on another view further down in the window.
I have looked into setting the scroll position directly, but haven't found any ways to do that.
How do I prevent the window to scroll when a view in the window scrolls?
I'm not sure the user will like to see a listbox animating itself other a thousand item.
Rather a little info panel on current processing displaying % progress, current item, time...
But but but.... i'm not here to question your design choices :-)
So what you should do is to get the scrollViewer of your listView, and call ScrollToVerticalOffset on it to have the content to scroll without stealing focus.
To find the scrollViewer, you might use my answer in this question :
How do I get the start index and number of visible items in a ListView?
If you want to go for a more xaml/binding solution, you might expose VerticalOffset using this kind of solution:
http://marlongrech.wordpress.com/2009/09/14/how-to-set-wpf-scrollviewer-verticaloffset-and-horizontal-offset/

Prevent ContextMenu on ListViewItem from disappearing when changing Filter

I have a ListView and TextBox for entering search text.
When the user searches for some text, I start a long-running search operation (on the UI thread, through a UI-thread-timer), and every now and again, while the search is still active, I change the ICollectionView.Filter property, to cause the ListView to refresh and present more items that the search has found as matching.
The problem is that if the user right clicks on one of the items, and then as Search is happening in the background, the tree is refreshed, then the ContextMenu disappears.
How can I prevent that from happening?
Here's my theory:
Because the whole tree is refreshed, WPF cannot know that the item (which was right-clicked) is the exact same item as after the refresh. Technically, that Item isn't even "there" anymore. It's really a new item with the exact same properties (from the ListView's perspective).
There are a some ways you can try to remediate this:
Capture the right-click of the item, store the item; capture the tree refresh, check if the item is equal to the stored item; if equal show the ContextMenu.
Instead of refreshing the whole tree, add items to the end of the list.
While ContextMenu is open, suspend refreshing the tree.

WPF: listview display recently added items on top instead of bottom

I got a collection bind to a listview. This collection gets items added every 4-5 seconds and new items will automatically be added in bottom of the listview. So if you gonna see newest items then you need to scroll down to the bottom.
My question is:
is it possible to like reverse the listview so the new newest items are on top and oldest items in bottom ?
Thanks
Use Insert instead of Add :
collection.Insert(0, newItem);
Note that it's slower than Add since it has to shift all items by 1 position. It might be an issue if the list is very big.
Even though you already voted the other answer as accepted, as they pointed out, while that is a simple, one-line solution, that does a lot of shifting and such which can affect performance.
IMHO, a better, correct way to do this would be to use sorting. It's easy to get the default view of a collection, then apply sorting to it, or just use a CollectionViewSource and apply sorting there. Of course that means you have to have a field to sort on, but that could be as simple as adding an index property if you don't already have a time-stamp or something similar.
Let me know if you want a code example here and I'll provide one.
Two approaches:
1) Use CollectionChange event to capture any item-change in the list.
Then sort the list in descending order and then render.
// subscribe to CollectionChanged event of the ObservableCollection
_recordingsHidden.CollectionChanged += RecordingsHiddenOnCollectionChanged;
private void RecordingsHiddenOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
var view = (CollectionView)CollectionViewSource.GetDefaultView(lvCallHistory.ItemsSource);
view.SortDescriptions.Add(new SortDescription("StartTime", ListSortDirection.Descending));
}
Here 'lvCallHistory' is my list view and sorting is done based on 'StartTime'.
This approach is effective only if the list contains less items. Because sorting is a costly operation and if you update the list frequently, then it might affect the performance of the application. The user may experience 2-3 seconds lag each time the view is rendered.
2) Avoid sorting: Every time you update the list, point the scroll bar to the last added item. (Pros: Very effective and no performance issue. Cons: Items still added to the end of list, even though scroll bar takes you to the new item whenever a new entry comes.)
lvCallHistory.SelectedItem = <your_collection>.Last();
lvCallHistory.ScrollIntoView(lvCallHistory.SelectedItem);
Set sorting order to descending and use add items
this.listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
listView1.Items.Add( listviewitem);
or remove sorting and insert item
listView1.Items.Insert(0, listviewitem);
either of them will work..

Categories