我可以以编程方式设置WPF ListBox的滚动条的位置吗?默认情况下,我希望它在中心.
要在ListBox中移动垂直滚动条,请执行以下操作:
命名列表框(x:Name ="myListBox")
为Window添加Loaded事件(Loaded ="Window_Loaded")
使用方法实现Loaded事件:ScrollToVerticalOffset
这是一个工作样本:
XAML:
Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni Zamboni
C#
private void Window_Loaded(object sender, RoutedEventArgs e) { // Get the border of the listview (first child of a listview) Decorator border = VisualTreeHelper.GetChild(myListBox, 0) as Decorator; if (border != null) { // Get scrollviewer ScrollViewer scrollViewer = border.Child as ScrollViewer; if (scrollViewer != null) { // center the Scroll Viewer... double center = scrollViewer.ScrollableHeight / 2.0; scrollViewer.ScrollToVerticalOffset(center); } } }