How to set ListBox's Scrollbar position?
I'm developing 开发者_开发百科a silverlight 4 application and I often use Listboxes and databinding. What I would like to do is set the scrollbar position to the bottom of my Listbox. Is there a simple way to do this?
By the way I've tried this but it doesn't work:
COTO_dg.ScrollIntoView(COTO_dg.Items[COTO_dg.Items.Count - 1]);
Thank You, Ephismen.
The code you posted works fine, but not right after the items are inserted into the ItemsControl. To make sure you give the control enough time to update itself, it's easier to use:
Dispatcher.BeginInvoke(() => lb.ScrollIntoView(lb.Items.Last());
where lb is a ListBox or any other ItemsControl. (this works in the constructor of a Silverlight page, right after some code adding a bunch of items, just tested).
Note: the references were the default ones inserted by Visual Studio:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
Alright, found something not very clean but it works. I'll post it so other people see how I did it:
Here is how I invoke the method:
Dispatcher.BeginInvoke(new lol(my_method));
I created an anonymous delegate and the corresponding method that I will call into it:
public delegate void lol();
public my_method()
{
COTO_dg.ScrollIntoView(COTO_dg.Items[COTO_dg.Items.Count - 1]);
}
Hope this helps someone.
精彩评论