WPF Listbox add item without scrolling - wpf

Given a WPF ListBox with a vertical scrollbar that uses databinding, is there a way to get it to not scroll when items are added to the bound collection?
Use case: User is scrolled down the list to an item and is reading it. New item is added to the head of the bound collection. Listbox should not scroll or otherwise "jump".
Currently, when I add an item to the head of the collection, the listbox scrolls the text down one item.

Related

A strange effect when click some space in ListViewItem

My ListView's ItemTemplate is a very complex Grid, which has multiple rows and columns. The ListView's SelectionMode is Extended.
I met a very strange effect when I click my ListViewItem now. When I click some specific space, such as two gridrows' gap, the item isn't been selected, the ListView will scroll up, then the mouse pointed the next item, no item is selected. This effect only occurs when I click the last item in the view of the ListView.
The ListViewItem's IsSelected property is bound to my model's IsSelected property, and the binding mode is two way. My ItemsSource is ObservableCollection testList, I also have a ObservableCollection which contains the Items which are selected. If double click the item, a new window will be open which display the details of the selected model.
Sometimes, when I single click the space which I mentioned, it even has effect of double click, but an exception is thrown, because more than one item in the selected collection.
Anybody met the strange problem?

Scroll Treeview to Bottom automatically in WPF

I got a treeview with a bunch of nodes where more gets added over time. When I add a new node to the treeview, I need to make sure that the very bottom most node is visible.
I tried using the ItemContainerGenerator to select the last item and bring it into view. But it doesn't work for me.
How can I make my treeview scroll to the very last item?
If you are working with TreeViewItem in code, I think you can use treeViewItem.BringIntoView() which should cause the scrollable area the item is in to scroll so that it is visible.
Edits
Examples
view.SchemaUpdateGrid.ScrollIntoView(e.UserState);
The above like is from a BackgroundWorker.ProgressChanged event handler. e.UserState contains the currently working object (custom class), not a grid row. The grid automatically translates the object into the item.

ComboBox selected item text disappears when scrolling listbox

I have Combo boxes inside a list box and whenever the list box is scrolled and the combo box is scrolled off of the screen the list box is firing a selection change event on the combo box and setting the selected index of the combo box to null.
If I scroll back and forth multiple times you will see the selected item display and be removed by scrolling the list back and forth.
Does anyone have an idea on ow to fix this? I need the combo box to retain the selected index.
I have even changed the collection that holds the Combo-box data to a list from an observable collection and it still does the same thing.
I am using silver light v4, .net 4
Thanks...
This is probably a result of the default virtualising nature of the ListBox. As items scroll off the displayed the items are actually removed from the Visual Tree. If you don't have too many items in the list set the ItemsPanel property of the ListBox to an ItemsPanelTemplate containing a simple StackPanel.
Better would be to cease using the selection change event in this scenario, use a binding on the SelectedItem property instead.
I had the same issue, but with a datagrid.
I tried this (preferable solution), but it didnt work for me.
Silverlight ComboBox and SelectedItem
So i had to go with this....
http://forums.silverlight.net/post/396922.aspx

Preserving a bound ListBox's scroll position when the underlying collection changes

I have a ScrollViewer and a ListBox inside it which is bound to an ObservableCollection in the view model. The ScrollViewer is maximized to take up all available space of the parent container. I'm finding that when the collection is modified and ends up producing more ListBoxItems than can fit in the viewable area of the ScrollViewer, the ScrollViewer scrolls down to show the last item in the ListBox. How do I prevent the ScrollViewer from scrolling when the child ListBox's items are updated?
I would like the scroll position to stay intact whenever the collection in the view model is updated.
Thanks in advance!
You are going to have to manage this yourself. The ListBox has a ScrollIntoView method that allows you to scroll to a specific location:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(v=VS.95).aspx
Determining the items that are currently visible, if you need this, is not so easy. See the ItemsControlExtensions that I wrote as part of the WP7Contrib project:
http://wp7contrib.codeplex.com/SourceControl/changeset/view/67473#1475881
This has a GetItemsInView extensions method that will provide the list of visible items.

Creating an infinite Silverlight ItemsControl

I'm interested in creating an ItemsControl for Silverlight that "loops" the items. When the user scrolls to the end of the list the ItemsControl should then show the first item(s) in the list. If the user scrolls above the first item in the list the ItemsControl should show the last item(s) in the list. It probably makes sense here for the ItemsControl to hold more items than it contains. What's a good way to go about creating this control? I'm planning to data bind the ItemsSource property so a custom collection could also work.
Turn to the good doctor for help: http://drwpf.com/blog/2009/08/05/itemscontrol-n-is-for-natural-user-interface/

Resources