Могу ли я программно установить положение полосы прокрутки WPF ListBox? По умолчанию я хочу, чтобы он шел по центру.
Установка положения полосы прокрутки ListBox
Ответы (5)
Чтобы переместить вертикальную полосу прокрутки в ListBox, сделайте следующее:
- Назовите свой список (x:Name="myListBox")
- Добавить событие Loaded для окна (Loaded="Window_Loaded")
- Реализовать событие 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);
}
}
}
person
Zamboni
schedule
12.06.2010
Dim cnt as Integer = myListBox.Items.Count
Dim midPoint as Integer = cnt\2
myListBox.ScrollIntoView(myListBox.Items(midPoint))
or
myListBox.SelectedIndex = midPoint
Это зависит от того, хотите ли вы, чтобы средний элемент отображался или был выбран.
person
Bob King
schedule
01.10.2008
это просто прокручивает его в поле зрения. Мне нужно, чтобы он прокручивался прямо к центру. Но спасибо
- person ScottG; 03.10.2008
Я только немного изменил код 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));
person
karol
schedule
25.06.2012
У меня есть ListView с именем MusicList. MusicList автоматически переходит к следующему элементу после воспроизведения музыки. Я создаю обработчик события Player.Ended следующим образом (а-ля Zamboni):
if (MusicList.HasItems)
{
Decorator border = VisualTreeHelper.GetChild(MusicList, 0) as Decorator;
if (border != null)
{
ScrollViewer scrollViewer = border.Child as ScrollViewer;
if (scrollViewer != null)
{
MusicList.ScrollIntoView(MusicList.SelectedItem);
}
}
}
Вы получаете следующий элемент, видимый внизу.
person
user1523904
schedule
05.12.2019
Я не думаю, что у ListBox есть это, но у ListViews есть EnsureVisible, который перемещает полосу прокрутки в нужное место, чтобы убедиться, что элемент отображается.
person
dguaraglia
schedule
01.10.2008
SureVisible - это функция windows.Forms, вопрос был о WPF. Насколько я могу судить, в WPF нет метода SureVisible.
- person Sam; 17.10.2008