설정의 스크롤바 위치의 ListBox
-
03-07-2019 - |
문제
나 할 수 있는 프로그래밍 방식으로의 위치를 설정 WPF ListBox 의 scrollbar?기본적으로,나는 그것에 가서 센터도 있습니다.
해결책
이동하는 수직 스크롤 막대 목록 상자에서는 다음을 수행:
- 이름 목록 상자에(x:Name="myListBox")
- 추가로드 이벤트에 대한 창문(로드="Window_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);
}
}
}
다른 팁
Dim cnt as Integer = myListBox.Items.Count
Dim midPoint as Integer = cnt\2
myListBox.ScrollIntoView(myListBox.Items(midPoint))
나
myListBox.SelectedIndex = midPoint
그것은 경우에 따라 달라집니다 당신이 원하는 중 항목을 보거나,선택됩니다.
나는 그냥 조금 변경하는 코드의 잠보고 추가되는 위치를 계산 합니다.
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));
나는 생각하지 않는 박해야하는지만,ListViews 가 EnsureVisible 방법 이동하는 스크롤바를 배치하기 위해 필요한지 확인 항목이 표시됩니다.
제휴하지 않습니다 StackOverflow