如何自动滚动列表框到底部wpf c#

本文关键字:底部 wpf 何自动 滚动 列表 | 更新日期: 2024-07-27 14:47:27

我有一个列表框,我动态地将项目添加到列表框中。

我希望列表框自动滚动到最后添加的项目。

我使用

List<string> ItemsList = new List<string>
public void InsertItem(string newItem)
    {
        ItemsList.Add(status);
        if (ItemsList.Count > MaxSize)
        {
            ItemsList.RemoveAt(0);
        }
        lb.Items.Refresh();
        lb.SelectedIndex = lb.Items.Count - 1;
        lb.ScrollIntoView(status);
    }

但这只在我的应用程序初始化之前有效(即,我在应用程序启动之前添加了一些项目)

但在应用程序启动后,如果我尝试添加项目,滚动条不会自动滚动到最后添加的项目

有谁能告诉这个的解决方案

如何自动滚动列表框到底部wpf c#

事实证明,这是一项相当艰巨的任务,因为ScrollIntoView只有在第一次被调用时才能工作。之后的每一个电话都会因为某种原因而无法工作。

解决方法是找到列表框的"ScrollInfo"并设置滚动值。参见以下示例

    public static void AutoScrollToCurrentItem(ListBox listBox, int index)
    {
        // Find a container
        UIElement container = null;
        for (int i = index; i > 0; i--)
        {
            container = listBox.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;
            if (container != null)
            {
                break;
            }
        }
        if (container == null) 
            return;
        // Find the ScrollContentPresenter
        ScrollContentPresenter presenter = null;
        for (Visual vis = container; vis != null && vis != listBox; vis = VisualTreeHelper.GetParent(vis) as Visual)
            if ((presenter = vis as ScrollContentPresenter) != null)
                break;
        if (presenter == null) 
            return;
        // Find the IScrollInfo
        var scrollInfo =
            !presenter.CanContentScroll ? presenter :
            presenter.Content as IScrollInfo ??
            FirstVisualChild(presenter.Content as ItemsPresenter) as IScrollInfo ??
            presenter;
        // Find the amount of items that is "Visible" in the ListBox
        var height = (container as ListBoxItem).ActualHeight;
        var lbHeight = listBox.ActualHeight;
        var showCount = (int)(lbHeight / height) - 1;
        //Set the scrollbar
        if (scrollInfo.CanVerticallyScroll)
           scrollInfo.SetVerticalOffset(index - showCount);
    }
    private static DependencyObject FirstVisualChild(Visual visual)
    {
        if (visual == null) return null;
        if (VisualTreeHelper.GetChildrenCount(visual) == 0) return null;
        return VisualTreeHelper.GetChild(visual, 0);
    }

使用上述代码的方法可以是这样的:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        listBox1.Items.Add("New Item");
        AutoScrollToCurrentItem(listBox1, listBox1.Items.Count);
    }

我真的希望我有足够的分数来评论你的问题。您的ItemsList是如何连接到ListBox的?启动应用程序后,是否可以手动向下滚动到新项目?

很可能您已将ItemsList绑定到ListBox。如果是,我建议您将List<string>更改为ObservableCollection<string>,以便ListBox可以自动更新自己。

ObservableCollection

如果你可以自动更新列表框,那么就使用ObservableCollection。

private ObservableCollection<string> _source  = new ObservableCollection<string>();

以及您的插入方法:

private void Insert(string item)
      {
         _source.Add(item);
         lb.SelectedIndex = _source.Count - 1;
         lb.ScrollIntoView(ListBox.SelectedItem);
      }