在Silverlight列表框中自动滚动

14 silverlight listbox

如何以编程方式强制Silverlight列表框滚动到底部,以便始终可以看到添加的最后一个项目.

我试过简单地选择这个项目.它最终选择但仍然不可见,除非您手动滚动到它.

Bil*_*iss 24

使用ListBox的ScrollIntoView方法传入最后一项.您可能需要立即调用UpdateLayout才能使其正常工作.


Bro*_*ing 7

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)