Wpf 设置列表框的滚动条位置

Wpf 设置列表框的滚动条位置,wpf,listbox,scrollviewer,Wpf,Listbox,Scrollviewer,我可以通过编程设置WPF列表框滚动条的位置吗?默认情况下,我希望它位于中间。我认为ListBox没有这种功能,但ListView有一种方法,可以将滚动条移动到所需的位置,以确保显示项目 Dim cnt as Integer = myListBox.Items.Count Dim midPoint as Integer = cnt\2 myListBox.ScrollIntoView(myListBox.Items(midPoint)) 或 这取决于您是否希望中间的项目刚刚显示或选中。要移动列表

我可以通过编程设置WPF列表框滚动条的位置吗?默认情况下,我希望它位于中间。

我认为ListBox没有这种功能,但ListView有一种方法,可以将滚动条移动到所需的位置,以确保显示项目

Dim cnt as Integer = myListBox.Items.Count
Dim midPoint as Integer = cnt\2
myListBox.ScrollIntoView(myListBox.Items(midPoint))


这取决于您是否希望中间的项目刚刚显示或选中。

要移动列表框中的垂直滚动条,请执行以下操作:

  • 命名您的列表框(x:Name=“myListBox”)
  • 为窗口添加已加载事件(Loaded=“Window\u Loaded”)
  • 使用方法实现加载的事件:ScrollToVerticalOffset
  • 以下是一个工作示例:

    XAML:


    我刚刚更改了Zamboni的一些代码,并添加了位置计算

    var border = VisualTreeHelper.GetChild(list, 0) as Decorator;
    if (border == null) return;
    var scrollViewer = border.Child as ScrollViewer;
    if (scrollViewer == null) return;
    scrollViewer.ScrollToVerticalOffset((scrollViewer.ScrollableHeight/list.Items.Count)*
                                        (list.Items.IndexOf(list.SelectedItem) + 1));
    

    我有一个名为MusicList的列表视图。MusicList在播放音乐后自动移动到下一个元素。我为玩家创建一个事件处理程序。结束事件如下(la Zamboni):

    if(MusicList.HasItems)
    {
    装饰器边框=VisualTreeHelper.GetChild(MusicList,0)作为装饰器;
    如果(边框!=null)
    {
    ScrollViewer ScrollViewer=border.Child作为ScrollViewer;
    如果(scrollViewer!=null)
    {
    MusicList.ScrollIntoView(MusicList.SelectedItem);
    }
    }
    }
    

    您可以在底部看到下一个元素。

    这只是将其滚动到视图中。我需要它向右滚动到中心。但是thanksEnsureVisible是一个windows.Forms函数,问题是关于WPF的。就我所知,在WPF中没有可保证的方法。
    <Window x:Class="ListBoxScrollPosition.Views.MainView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Loaded="Window_Loaded"
      Title="Main Window" Height="100" Width="200">
      <DockPanel>
        <Grid>
          <ListBox x:Name="myListBox">
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
            <ListBoxItem>Zamboni</ListBoxItem>
          </ListBox>
        </Grid>
      </DockPanel>
    </Window>
    
    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);
        }
      }
    }
    
    var border = VisualTreeHelper.GetChild(list, 0) as Decorator;
    if (border == null) return;
    var scrollViewer = border.Child as ScrollViewer;
    if (scrollViewer == null) return;
    scrollViewer.ScrollToVerticalOffset((scrollViewer.ScrollableHeight/list.Items.Count)*
                                        (list.Items.IndexOf(list.SelectedItem) + 1));