简体   繁体   English

ListBox.ScrollIntoView() 在 WP7 中似乎不起作用

[英]ListBox.ScrollIntoView() does not seem to work in WP7

I want to move the listbox scrollbar to the bottom whenever a new item is added to the itemssource, but ScrollIntoView() doesn't seem to do anything if I pass it either a reference to the newly added item, or the index of it.每当将新项目添加到项目源时,我都想将列表框滚动条移动到底部,但是如果我将其传递给新添加项目的引用或其索引,则ScrollIntoView()似乎没有任何作用。 Has anyone gotten this to work, or have any other suggestions as to how I could scroll the listbox down to the bottom?有没有人让它起作用,或者对我如何将列表框向下滚动到底部有任何其他建议?

Some code:一些代码:

    void Actions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //if a new item was added, set it to the selected item
        if (e.NewItems != null)
        {
            MainListBox.SelectedIndex = e.NewStartingIndex;

            //MainListBox.ScrollIntoView(MainListBox.Items.Last());     //todo: this doesnt seem to work
        }
    }

MSDN says: MSDN 说:

When the contents of the ItemsSource collection changes, particularly if many items are added to or removed from the collection, you may need to call UpdateLayout() prior to calling ScrollIntoView for the specified item to scroll into the viewport.当 ItemsSource 集合的内容发生更改时,尤其是在集合中添加或删除了许多项时,您可能需要在调用 ScrollIntoView 之前调用 UpdateLayout() 以将指定项滚动到视口中。

Could that be your problem?那可能是你的问题吗?

THIS is the answer:这是答案:

http://dotnet-experience.blogspot.com.es/2010/12/wpf-listview-scrollintoview.html http://dotnet-experience.blogspot.com.es/2010/12/wpf-listview-scrollintoview.html

In a few words: the items are loaded into the ListBox asynchronously, so if you call ScrollIntoView() within the CollectionChanged event (or similar) it will not have any items yet, so no scrolling.简而言之:项目是异步加载到 ListBox 中的,因此如果您在 CollectionChanged 事件(或类似事件)中调用 ScrollIntoView() ,它将不会有任何项目,因此不会滚动。

Hope it helps, it surely helped me!希望它有帮助,它肯定对我有帮助! ;-) ;-)

ScrollIntoView definitely works. ScrollIntoView绝对有效。 I just added an application button to an empty databound app and doing the following on button click caused the list to scroll.我刚刚向一个空的数据绑定应用程序添加了一个应用程序按钮,并在单击按钮时执行以下操作导致列表滚动。

MainListBox.ScrollIntoView(MainListBox.Items.Last());

Could be an issue with an event on selectionChanged ?可能是selectionChanged事件的问题吗? Do you have anything hooked up to that?你有什么与之相关的吗?
Does the ScrollIntoView work if you don't set the selected item?如果您不设置所选项目,ScrollIntoView 是否有效?

you can insert the new item always on the top by您可以始终在顶部插入新项目

yourItemList.Insert(0, item); yourItemList.Insert(0, item);

thus no need for ScrollIntoView.因此不需要 ScrollIntoView。 May be this will help!!可能这会有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM