Установка позиции полосы прокрутки ListBox

Вы показали, что отправили в функцию цикла запуска графический интерфейс создания объекта , а затем добавили метод оплаты доступа доступа к функции

, теперь вы получаете доступ к классу insted объекта

10
задан Dave Clemmer 26 July 2011 в 18:45
поделиться

3 ответа

Чтобы переместить вертикальную полосу прокрутки в ListBox, сделайте следующее:

  1. Назовите свой список (x: Name = "myListBox")
  2. Добавьте событие Loaded для окна (Loaded = "Window_Loaded")
  3. Реализовать событие Loaded с помощью метода: ScrollToVerticalOffset

Вот рабочий пример:

XAML:

<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>

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);
    }
  }
}
7
ответ дан 4 December 2019 в 01:58
поделиться
Dim cnt as Integer = myListBox.Items.Count
Dim midPoint as Integer = cnt\2
myListBox.ScrollIntoView(myListBox.Items(midPoint))

или

myListBox.SelectedIndex = midPoint

Это зависит от того, если Вы хотите средний объект, просто показанный или выбранный.

3
ответ дан 4 December 2019 в 01:58
поделиться

Я не думаю, что ListBoxes имеют это, но ListViews имеют метод EnsureVisible, который перемещает полосу прокрутки в место, необходимое, чтобы удостовериться, что объект показывают.

-1
ответ дан 4 December 2019 в 01:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: