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.

image

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

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • Live
  • Reddit
  • TwitThis
  • email
  • Slashdot
  • StumbleUpon

You may also be interested with:

  1. INotifyPropertyChanged auto wiring or how to get rid of redundant code
  2. Real singleton approach in WPF application

8 Responses to “Auto scroll ListBox in WPF”

  1. Andrej Tozon Says:

    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.

  2. 2008 November 11 - Links for today « My (almost) Daily Links Says:

    Pingback from  2008 November 11 – Links for today &laquo; My (almost) Daily Links

  3. Shaun Venus Says:

    Is the source available so I can try this out?

    I see the ‘Source code for this article is attached’ text but no link.

  4. Tamir Says:

    Yes, this is my fault. But you not really need the source, all necessary information is in the article.

  5. BadGirl36 Says:

    Validate the length of every buffer before accessing it. ,

  6. Jamie Wink Says:

    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.

  7. Wolfgang Says:

    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…

  8. Товары для детей − Конверт детский «VENUS BABY COAT». Голубой. MORA Says:

    [...] Auto scroll ListBox in WPF | Tamir Khason – Just code [...]

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together