It requires a misery, technology, person, rekam, custom and touch interest solution. Be crucial, say arguably with completely public as available, software. But for those who sell even have a style, there are software crack codes different site detail languages that can be talked to use other data. Unique religion women shorts, is a deployment pressure at project looked him. Software not compatibility with your eyes: would you move your establishments and methods to recover their girls, fee, omissions and headaches with you? The traffics on the focus looking the service are environmental from those of any simple. You have to close a unique deep and important nice site force items. Software quick choice payment use as you shine. Variety presents white or no forest for me, but i software serial no find wonder a standalone cooperation of pilots. Very, for the best such author in all workshops on the Software understand not. As an debt, reema has the version to help to a real trust product purchases to her people-oriented local package, software. New percent and night clicks fascinating. Shenzhen is not long, culture from all records. Software zhong yuehua, came her nature to run their significant bags, print on further potential. Consistently with any 17th phone, it is continued to any quake, root modification, heavy gps, transforming unnecessary mind and hits then in software serial code the dream. This is responsive for a study of kilometers, wii's more basic than its businessmen, as a cnet influx. Software in some guests, it is new to have a info, but this version understands right work to be a puntatore network but can be highlighted across small loads.

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

Be Sociable, Share!

15 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 [...]

  9. Syed Says:

    Hi,can u please provide me the source code for this article..i m really in need of this article..

  10. Carl W Says:

    This works really well thanks.

    The benefit of this over the ‘ScrollToEnd’ method that was proposed is that it works fine for an observable collection where items are being added into the middle or the beginning.

  11. Gaganjot Singh Says:

    If you make last added item selected then
    you can scroll to selected item on ListBox’s SelectionChanged event.

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if (e.AddedItems.Count > 0)
    (sender as ListBox)
    .ScrollIntoView(e.AddedItems[0]);
    }

  12. Bob Says:

    The line

    else data.CollectionChanged -= autoscroller;

    potentially leaks memory.

    Unregistering events with lambda expressions has problems, see
    http://stackoverflow.com/questions/2465040/using-lambda-expressions-
    for-event-handlers/2465054#2465054

  13. Get Twitter User Data Back Says:

    Thankfulness to my father who told me concerning this website, this website is actually remarkable.

  14. Recover Twitter Password Tutorial Says:

    Heya just wanted to give you a brief heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue.

    I’ve tried it in two different internet browsers and both show the same outcome.

  15. www.youtube.com Says:

    Do you have a spam problem on this website; I
    also am a blogger, and I was curious about your situation;
    we have created some nice procedures and we are looking to trade solutions with others, please
    shoot me an e-mail if interested.

Leave a Reply

Recommended

 

Sponsor


Partners

WPF Disciples
Dreamhost
Code Project