Scroll Treeview to Bottom automatically in WPF - 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.

Related

WPF C# : Scroll TreeView Children instead of scrolling the entire container

I'm having a problem with a TreeView in WPF C#.
I have a TreeView with two TreeViewItems defined in XAML.
On the second TreeViewItem, I have a list of items defined by a binding.
When the list is too long, a vertical scrollbar appears and allows the entire TreeView to scroll.
But the two TreeViewItems must stay at the same place, so I'd like the content of the second to scroll instead, rather than the entire TreeView.
Left : what I get, and the two first items are scrolled out; Right what I'd like, with the two first items staying at the same spot. (The scrollbar is the red zone).
I look forward to hearing from you guys, and thanks in advance,
Raekh.
EDIT :
Found a solution : I removed the two treeviewitems and used a treeview with a item source obtained by binding, so the treeview starts after the treeviewitems and is scrolled properly.

ListView - Selected Item in the middle

I'm building a Windows Store app using XAML. I have a scrollable ListView that has its SelectedItem set on start.
Now, I have two requirements:
I would like the selected item to be always visible
In addition it
should be displayed in the middle of the list when possible (it's
not possible for the first element).
The first requirement I can partially fulfill by using list's LayoutUpdated event and list.ScrollIntoView(item) method, but this doesn't allow me to scroll down the list manually until the item is unselected.
For the 2nd requirement I have no ideas.
Ok, found out how to achieve that: http://fczaja.blogspot.com/2013/05/xaml-listview-scroll-selecteditem-to.html

Mouse Wheel in Nested Listboxes

I am currently working on a WPF project in C# using MVVM. In this project I have a list box that loads a user control for each row of data in my items source.
The user control that gets added for each item in my collection also contains a list box that is filled from a database. This second list box often grows and therefore makes each item larger than the view of the first listbox.
My question is, how to I make my listbox have smooth scrolling. Right now it jumps from the top of one item the the top of the next. Since each item is bigger than the view of the listbox I need to be able to scroll without jumping from the top of each item to the next.
I have used a scrollview to achieve this but the list box then intercepts the mouse wheel event and will not allow me to scroll without hovering over the scroll bar.
Is there a better way to do this?
UPDATE:
I can get smooth scrolling using the scrollview control but I cannot scroll while my mouse is inside the scrollview. I have to have my mouse over the scrollbar in order to scroll.
It is like something is intercepting my mousewheel event.
I was able to achieve what I was trying to do by using a ItemsControl rather than the ListBox. It loaded just like the ListBox control and pulled all of my data into it and let the ScrollView handle the scrolling. It did not intercept the mousewheel even like the listbox did.
If you do not need to be able to select an item, I highly recommend using the ItemsControl.
You can set on the Listbox the following Property ScrollViewer.CanContentScroll="False".

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.

SL3 TreeView Not Programmatically Scrolling

I have a treeview in Silverlight 3.
The treeview is bound to a observable collection - which contains a list of hierarchical data.
When the page loads initially, all nodes, by default, in the treeview, are collapsed.
I have functionality that allows a certain item in the treeview to be selected programmatically.
The problem that I am running into is when an item is selected that isn't immediately visible (i.e. one or more parent nodes are collapsed). I programmatically expand them, but, when I try to programmatically scroll into view, so the user can see the selected item, it doesn't work.
I looked into this further, and I believe that it has to do with the calculated viewport height for the scroll viewer. It almost seems like a timing issue, as, if the item's parent node is expanded, and then the item is programmatically selected, the code that scrolls the treeview into view for that selected treeview item works perfectly.
Please refer to the extension method below that I am using to scroll the treeview into view. Any help or suggestions on how to correct this would be greatly appreciated.
Thanks.
public static void BringIntoViewForScrollViewer(this FrameworkElement frameworkElement, ScrollViewer scrollViewer)
{
var transform = frameworkElement.TransformToVisual(scrollViewer);
var positionInScrollViewer = transform.Transform(new Point(0, 0));
if (positionInScrollViewer.Y < 0 || positionInScrollViewer.Y > scrollViewer.ViewportHeight)
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + positionInScrollViewer.Y - ScrollPadding);
}
Call UpdateLayout on the TreeView or ScrollViewer in between expanding the nodes and calling your extension method to ensure the VerticalOffset and ViewportHeight properties are up to date.
(answer copied from comment by Dan Auclair that led to problem resolution)

Resources