Scroll WPF Listview to specific line

前端 未结 11 1000
孤城傲影
孤城傲影 2020-11-30 02:34

WPF, Browserlike app.
I got one page containing a ListView. After calling a PageFunction I add a line to the ListView, and want to scroll the new line into view:

11条回答
  •  执笔经年
    2020-11-30 03:13

    I think the problem here is that the ListViewItem is not created yet if the line is not visible. WPF creates the Visible on demand.

    So in this case you probably get null for the item, do you? (According to your comment, you do)

    I have found a link on MSDN forums that suggest accessing the Scrollviewer directly in order to scroll. To me the solution presented there looks very much like a hack, but you can decide for yourself.

    Here is the code snippet from the link above:

    VirtualizingStackPanel vsp =  
      (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost",
       BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic, null, 
       _listView, null);
    
    double scrollHeight = vsp.ScrollOwner.ScrollableHeight;
    
    // itemIndex_ is index of the item which we want to show in the middle of the view
    double offset = scrollHeight * itemIndex_ / _listView.Items.Count;
    
    vsp.SetVerticalOffset(offset);
    

提交回复
热议问题