Hi guys,
I am trying to achieve a scrolling effect (please see fig. below) with ListBox.ScrollIntoView.
However, instead of Scrolling Into View, the items jumped into view. 2 questions:
1, Did I miss something with ListBox.ScrollIntoView?
2. What is the proper way to achieve the scrolling effect?
Thanks.
Here is the xaml
<Window x:Class="ScrollIntoView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"><Grid><ListBox x:Name="ListBox1" ScrollViewer.HorizontalScrollBarVisibility="Hidden" HorizontalAlignment="Left" Height="30" Margin="218,140,0,0" VerticalAlignment="Top" Width="47"><ListBox.ItemsPanel><ItemsPanelTemplate><StackPanel Orientation="Horizontal"/></ItemsPanelTemplate></ListBox.ItemsPanel><ListBoxItem FontSize="16">1</ListBoxItem><ListBoxItem FontSize="16">2</ListBoxItem><ListBoxItem FontSize="16">3</ListBoxItem><ListBoxItem FontSize="16">4</ListBoxItem><ListBoxItem FontSize="16">5</ListBoxItem><ListBoxItem FontSize="16">6</ListBoxItem><ListBoxItem FontSize="16">7</ListBoxItem><ListBoxItem FontSize="16">8</ListBoxItem></ListBox><Button x:Name="Btn_MoveRight" Content="Move Right" HorizontalAlignment="Left" Height="20" Margin="275,140,0,0" VerticalAlignment="Top" Width="70" Click="Btn_MoveLeft_Click"/><Button x:Name="Btn_MoveLeft" Content="Move Left" HorizontalAlignment="Left" Height="20" Margin="135,140,0,0" VerticalAlignment="Top" Width="70" Click="Btn_MoveRight_Click"/></Grid></Window>
Here is the xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ScrollIntoView { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Btn_MoveLeft_Click(object sender, System.Windows.RoutedEventArgs e) { ListBox1.ScrollIntoView(ListBox1.Items[6]); } private void Btn_MoveRight_Click(object sender, System.Windows.RoutedEventArgs e) { ListBox1.ScrollIntoView(ListBox1.Items[0]); } } }