Phương thức ScrollIntoView() sẽ cuộn mục cuối cùng vào dạng xem, tuy nhiên listBox.UpdateLayout() phải được gọi ngay trước ScrollIntoView(). Dưới đây là một phương pháp hoàn toàn với mã:
// note that I am programming Silverlight on Windows Phone 7
public void AddItemAndScrollToBottom(string message)
{
string timestamp = DateTime.Now.ToString("mm:ss");
var item = new ListBoxItem();
item.Content = string.Format("{0} {1}", timestamp, message);
// note that when I added a string directly to the listbox, and tried to call ScrollIntoView() it did not work, but when I add the string to a ListBoxItem first, that worked great
listBoxEvents.Items.Add(item);
if (listBoxEvents.Items.Count > 0)
{
listBoxEvents.UpdateLayout();
var itemLast = (ListBoxItem)listBoxEvents.Items[listBoxEvents.Items.Count - 1];
listBoxEvents.UpdateLayout();
listBoxEvents.ScrollIntoView(itemLast);
}
}
+1 để chỉ ra cách Silverlight bỏ qua tính năng cơ bản nhất trên ListBox mà tôi có thể tưởng tượng. WinForms đã có nó mãi mãi! Xấu hổ bạn thực sự không phải là thành viên tại thời điểm này :) –