1

I'm trying to keep the scroll position of TreeView control even when an item is inserted.
ItemsSource in the TreeView is set by code and it shows items using DataTemplate.

When I insert an item to the data source at 0, the TreeView will automatically scroll up by the height of the new item.
Actually this feature is useful when scroll-position is at the top, but even if not it scrolls up.
(I'm using TreeView for Virtualizing items with CanContentScroll set to false.)

How can I prevent this default behavior?


WPF 4.0 / C# - Visual Studio 2010 on Windows 7

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
chitoku
  • 11
  • 1
  • 3
  • Not related but since you mentioned, one info - `Setting CanContentScroll to false disables UI virtualization`. – Rohit Vats Dec 30 '13 at 13:28
  • I set VirtualizingPanel as the ItemsPanel and VirtualizingPanel.IsVirtualizing to true. It makes TreeView UI virtualized one. (Not affects on ListBox / ListView) Sorry for my less information, thanks. – chitoku Dec 31 '13 at 00:30
  • That's fine but setting `CanContentScroll` will disable virtualization. Read more [here](http://stackoverflow.com/questions/3724593/why-setting-scrollviewer-cancontentscroll-to-false-disable-virtualization). – Rohit Vats Dec 31 '13 at 06:04
  • As a result, setting of `CanContentScroll` was ignored because of customizing template. I didn't know that behavior in case of using `VirtualizingStackPanel` as `ItemsPanelTemplate`. (This is maybe because `VirtualizingStackPanel` always uses pixel-based scrolling and `CanContentScroll` doesn't make sense.) – chitoku Dec 31 '13 at 11:58

2 Answers2

1

I had this problem a while ago, and couldn't find a "solution" for the actual issue, but I found a workaround to it.

Find the event where an item is added, and when that is triggered, set the selected item index to that of the amount of items -1 (select the last item).

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
XtrmJosh
  • 889
  • 2
  • 14
  • 33
  • SelectedIndex is not defined in TreeView control and SelectedItem is readonly. How can I solve it? Thanks. – chitoku Dec 31 '13 at 00:28
  • I haven't used the TreeView control a lot, but perhaps this may be of use? There must be a programatic way of selecting a node, and I suspect you might need to set an "IsSelected" property... http://www.daniweb.com/software-development/csharp/threads/352865/selecting-a-child-node-in-treeview-by-index – XtrmJosh Dec 31 '13 at 00:42
  • I tried to select the last item by `IsSelected` property of `TreeView.ItemContainerGenerator.ContainerFromIndex` but it still scrolls up to the top. I made sure that the event is fired and selection is successfully done. (TreeView.SelectedItem has been changed.) Any ideas? Thanks a lot. – chitoku Dec 31 '13 at 12:30
  • I'm all out of ideas beyond that, sorry... I think I actually had it with a ListBox, so I could simply set the SelectedIndex. All I can offer further is advice to find something about dynamically selecting TreeView items... Good luck! – XtrmJosh Dec 31 '13 at 16:48
0

Finally I solved this problem by handling ScrollChanged event of ScrollViewer.
Then write in codes like this:

var border = VisualTreeHelper.GetChild(sender as TreeView, 0) as Border;
var scrollViewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;

// Set the last vertival offset at global variable.
LastVerticalOffset = e.VerticalOffset;
scrollViewer.ScrollToVerticalOffset(e.VerticalOffset + e.ExtentHeightChange);

Thank you all.

chitoku
  • 11
  • 1
  • 3