如何以编程方式强制Silverlight列表框滚动到底部,以便始终可以看到添加的最后一个项目.
我试过简单地选择这个项目.它最终选择但仍然不可见,除非您手动滚动到它.
ScrollIntoView()方法将最后一项滚动到视图中,但是必须在ScrollIntoView()之前调用listBox.UpdateLayout().这是一个完整的代码方法:
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12572 次 |
| 最近记录: |