WPF Listbox Scrollintoview


This article is written by Pon Saravanan  on 10-Nov-10 Last modified on :08-Feb-11





VB.Net Tutorial - WPF Listbox using Scrollintoview

Scrolling the WPF listbox is not that easy as we do in Windows forms applications. But the same time it is not impossible also. With the help of Scrollintoview we can achieve scrolling.

Scrollintoview Function

WPF controls are little different than normal web form controls and windows forms controls.  The list box in windows can be scrolled by changing the selected index easily. But in wpf selection and scrolling are handled differently. So in order to scroll while selecting the control we need to write codes separately.

Note

Another point to note is, though the following code snippet will work as is. It is not working in a specific scenario, if the item you would like to scroll to is loaded inside the list box but not visible on the screen, then the scroll won’t work. I am finding a solution. From the initial analysis, the following seems to be working

Begin Invoke

Private Delegate Sub ScrollDelegate(ByVal ItemIndexToBeScrolled As Integer)

Dispatcher.BeginInvoke(New ScrollDelegate(AddressOf ScrollFeedbackViewToLastItem), New Object() {IndexToScroll})

Private Sub ScrollFeedbackViewToLastItem(ByVal IndexToScroll As Integer)
        If (IndexToScroll >= lstSongs.Items.Count) Then Return
        lstSongs.ScrollIntoView(lstSongs.Items(IndexToScroll))
End Sub

Screen Capture


 

Source code

Markup (.xaml)

    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="697" Name="Window1">
        <Grid>
            <ListBox Margin="10,10,101,11" Name="ListBox1" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding ProductName}">
                           
                        </Label>
                    </DataTemplate>
                </ListBox.ItemTemplate>
               
            </ListBox>
            <ScrollBar HorizontalAlignment="Right"
                    Margin="0,42,38,47" Name="ScrollBar1" Width="18" />
            <Button Height="23" HorizontalAlignment="Right"
                    Margin="0,10,17,0" Name="btnTop"
                    VerticalAlignment="Top" Width="75">Top</Button>
            <Button Height="23" HorizontalAlignment="Right"
                    Margin="0,0,17,20" Name="btnBottom"
                    VerticalAlignment="Bottom" Width="75">Bottom</Button>
        </Grid>
    </Window>

Code behind (xaml.Vb)

Class Window1
    Private Sub Window1_Loaded(ByVal sender As System.Object, _
                    ByVal e As RoutedEventArgs) Handles MyBase.Loaded
        Dim NorthwindData As New NorthwindDataContext
        ListBox1.ItemsSource = NorthwindData.Products
        ScrollBar1.SmallChange = 1
        ScrollBar1.LargeChange = NorthwindData.Products.Count / 10
        ScrollBar1.Minimum = 0
        ScrollBar1.Maximum = NorthwindData.Products.Count
    End Sub
    Private Sub ScrollBar1_ValueChanged(ByVal sender As System.Object, _
                                        ByVal e As RoutedPropertyChangedEventArgs(Of System.Double)) _
                                        Handles ScrollBar1.ValueChanged
        ScrollToIndex(e.NewValue)
    End Sub
    Private Sub ScrollToIndex(ByVal Index As Integer)
        If (Index >= ListBox1.Items.Count) Then Return
        ListBox1.ScrollIntoView(ListBox1.Items(Index))
    End Sub
    Private Sub btnTop_Click(ByVal sender As System.Object, _
                             ByVal e As RoutedEventArgs) Handles btnTop.Click
        ScrollToIndex(0)
    End Sub
    Private Sub btnBottom_Click(ByVal sender As System.Object, _
                                ByVal e As RoutedEventArgs) Handles btnBottom.Click
        ScrollToIndex(ListBox1.Items.Count - 1)
    End Sub
End Class

 

 

 











Comments

Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)