Auto scroll ListBox in WPF
In WinForms era it was very simple to autoscroll listbox content in order to select last or newly added item. It become a bit complicated in WPF. However, complicated does not mean impossible.
As for me, Microsoft should add this feature to base ListBox implementation as another attempt to be attractive environment for LOB application. See, for example this thread from MSDN forums. I’m really understand this guy. He do not want to implement it with a lot of code, he just want it to be included in core WPF control (but he should mark answers)
Generally, the simplest way to it is by using attached properties. So, your code will look like this
<ListBox Height=”200″ l:SelectorExtenders.IsAutoscroll=”true” IsSynchronizedWithCurrentItem=”True” Name=”list”/>
But what’s going on under the hoods? There it bit complicated
First of all, we should create attached property, named IsAutoscroll
public class SelectorExtenders : DependencyObject {
public static bool GetIsAutoscroll(DependencyObject obj) {
return (bool)obj.GetValue(IsAutoscrollProperty);
}public static void SetIsAutoscroll(DependencyObject obj, bool value) {
obj.SetValue(IsAutoscrollProperty, value);
}public static readonly DependencyProperty IsAutoscrollProperty =
DependencyProperty.RegisterAttached(“IsAutoscroll”, typeof(bool), typeof(SelectorExtenders), new UIPropertyMetadata(default(bool),OnIsAutoscrollChanged));
now handle it when you set it’s value by handling new items arrivals, set current and then scroll into it
public static void OnIsAutoscrollChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) {
var val = (bool)e.NewValue;
var lb = s as ListBox;
var ic = lb.Items;
var data = ic.SourceCollection as INotifyCollectionChanged;var autoscroller = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(
(s1, e1) => {
object selectedItem = default(object);
switch (e1.Action) {
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Move: selectedItem = e1.NewItems[e1.NewItems.Count - 1]; break;
case NotifyCollectionChangedAction.Remove: if (ic.Count < e1.OldStartingIndex) { selectedItem = ic[e1.OldStartingIndex - 1]; } else if (ic.Count > 0) selectedItem = ic[0]; break;
case NotifyCollectionChangedAction.Reset: if (ic.Count > 0) selectedItem = ic[0]; break;
}if (selectedItem != default(object)) {
ic.MoveCurrentTo(selectedItem);
lb.ScrollIntoView(selectedItem);
}
});if (val) data.CollectionChanged += autoscroller;
else data.CollectionChanged -= autoscroller;}
That’s all. Also it can be done by visual tree querying (as thread started proposed). Find scrollviewer inside the ListBox visual tree and then invoke “ScrollToEnd” method of it.
Have a nice day and be good people. And, yes, WPF development team should consider this feature implemented internally for any Selector and ScrollViewer control
Source code for this article is attached
You may also be interested with:
- INotifyPropertyChanged auto wiring or how to get rid of redundant code
- Real singleton approach in WPF application
November 9th, 2008 · Comments (8)
8 Responses to “Auto scroll ListBox in WPF”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:59 am
That’s a great tip! I’m using a very similar method, but for slightly different purpose: I sometimes need just an ItemsControl, wrapped in a ScrollViewer (not a Selector), which needs to scroll to the end, when dependent data source is changed. In this case, I define an INotifyCollectionChanged type attached property, which is bound to the data source. The ItemsControl Control template now looks somewhat like this:
<ControlTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto" controls:ScrollViewerExtender.AutoScrollSource="{Binding DataSource}">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
This approach obviously also factors out the need to find the ScrollViewer, but requires to mess with the control template.
January 1st, 2009 at 12:59 am
Pingback from 2008 November 11 – Links for today « My (almost) Daily Links
February 5th, 2009 at 2:14 am
Is the source available so I can try this out?
I see the ‘Source code for this article is attached’ text but no link.
February 5th, 2009 at 6:49 am
Yes, this is my fault. But you not really need the source, all necessary information is in the article.
October 22nd, 2009 at 5:04 pm
Validate the length of every buffer before accessing it. ,
November 28th, 2009 at 3:26 am
Hello, I have just come across your blog whilst browsing on online as I’m seeking some material on engine lifts!. It is a good blog so I have bookmarked you and will revisit another day to give it a more detailed look when I have more time.
January 15th, 2010 at 12:56 am
Great Tip!!! Just starting up with WPF and I’m starting to notice the differences to WinForms and Aps straight away… Trying to suck up as many tutorials / Tips as pos…
February 17th, 2010 at 12:48 pm
[...] Auto scroll ListBox in WPF | Tamir Khason – Just code [...]