Scroll WPF Listview to specific line

前端 未结 11 1036
孤城傲影
孤城傲影 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:06

    If you just want to show and focus the last item after creating a new data item, this method is maybe better. Compare to ScrollIntoView, ScrollToEnd of ScrollViewer is in my tests more reliable. In some tests using ScrollIntoView method of ListView like above failed and i don't know reason. But using ScrollViewer to scroll to last one can work.

    void FocusLastOne(ListView lsv)
    {
       ObservableCollection items= sender as ObservableCollection;
    
       Decorator d = VisualTreeHelper.GetChild(lsv, 0) as Decorator;
       ScrollViewer v = d.Child as ScrollViewer;
       v.ScrollToEnd();
    
       lsv.SelectedItem = lsv.Items.GetItemAt(items.Count - 1);
       ListViewItem lvi = lsv.ItemContainerGenerator.ContainerFromIndex(items.Count - 1) as ListViewItem;
       lvi.Focus();
    }
    
        

    提交回复
    热议问题