?
Solved

Autoscroll a ListBox with databinding

Posted on 2011-09-08
4
Medium Priority
?
633 Views
Last Modified: 2013-11-12
Hello everybody

I got a ListBox with a databinding and I would like that to automaticly scroll to the bottom when a new item is added.

I know that I can use the ScrollIntoView() method to scroll the ListBox. But I can't use this, because the ListBox gets its items by a databinding.

Does anyone know a solution to my problem?

Thank you for your help!
Urs
0
Comment
Question by:innovasoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36508101
Is it SL or WPF? I tried this in WPF with databound ListBox and it works for me.

YourListBox.ScrollIntoView(YourListBox.Items[YourListBox.Items.Count - 1]);
YourListBox.SelectedIndex = YourListBox.Items.Count - 1;
0
 
LVL 1

Author Comment

by:innovasoft
ID: 36509677
oh, I forgot to tell: it's Silverlight

yes, I know that it works with ScrollIntoView. But the problem is, that I have the ListBox in the view bound to a property in the model. I did this, that I don't have to refresh the listbox manually. But now, the view doesn't really have an event when the listbox is updated with a new item. So there is no moment when I can use this ScrollIntoView.

Or is there something like an event "ItemsUpdated"?
0
 
LVL 8

Accepted Solution

by:
jagrut_patel earned 2000 total points
ID: 36509774
In SL,

1. I bound the ListBox with an ObservableCollection, like
    private ObservableCollection<Employee> employeeList;
    lstEmployees.ItemsSource = employeeList;

2. After new item is added into employeeList I wrote

lstEmployees.ScrollIntoView(lstEmployees.Items[lstEmployees.Items.Count - 1]);
lstEmployees.SelectedIndex = lstEmployees.Items.Count - 1;


It works perfectly for me. Last items gets selected. Is there anything I'm doing differently than what you need?
0
 
LVL 1

Author Comment

by:innovasoft
ID: 36511159
yes, I think there is a difference. I have separated the model and the view. So the employeeList would be in the model while the ListBox is in the view. Since the model doesn't know the view, it can't do the lstEmployees.ScrollIntoView method.

But as I'm writing this, I think I could fire a custom event such as newItemAdded. The view should listen to that so I could put this code in the view anyway :)

Thank you for your help!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question