wpf 设置Listbox的滚动条位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14318069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:01:11  来源:igfitidea点击:

Setting the scrollbar position of Listbox

c#wpflistbox

提问by lexeRoy

Possible Duplicate:
Setting the scrollbar position of a ListBox

可能的重复:
设置 ListBox 的滚动条位置

How can I set WPF listbox scroll bar position to the last item added?

如何将 WPF 列表框滚动条位置设置为最后添加的项目?

回答by Clemens

You could simply use the ScrollIntoViewmethod:

您可以简单地使用ScrollIntoView方法:

object item = ...
listBox.Items.Add(item);
listBox.ScrollIntoView(item);

回答by Ionic? Biz?u

In WPF you have to use ScrollIntoView. I added this example:

在 WPF 中,您必须使用ScrollIntoView. 我添加了这个例子:

int i = 0;
private void button1_Click(object sender, RoutedEventArgs e)
{   
   listBox1.Items.Add("Item nr. " + i.ToString());
   listBox1.ScrollIntoView("Item nr. " + i.ToString());
   i++;
}

In Windows Forms you have this:

在 Windows 窗体中,您有这个:

int visibleItems = myListBox.ClientSize.Height / myListBox.ItemHeight;
myListBox.TopIndex = Math.Max(myListBox.Items.Count - visibleItems + 1, 0);

回答by Gishu

You can do it in a MVVM-aligned way using the ideas explained in this post- just map to a ListViewItem instead of a TreeViewItem (and instead of IsSelected being a trigger, you can set it to the new item being added).

你可以做到这一点,使用的想法解释一个MVVM对齐方式这篇文章-只是映射到ListViewItem的,而不是一个TreeViewItem(和而不是IsSelected被触发,你可以将其设置为添加新项目)。