Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1072
  • Last Modified:

WPF Listbox Scroll to Bottom VB.Net

I am developing a WPF application that has a listbox.  In a standard windows form when I added an item to a listbox I could just use the TopIndex feature but that is not available in a WPF window.  

I have tried ScrollIntoView but I cannot get it to work.  Any help would be appreciated.

Thanks!
Public Sub UpdateStatus(ByVal s As String)
        Dim currDateTime As DateTime = DateTime.Now
        Dim sDateTime As String

        If (s <> gsLine) Then
            sDateTime = currDateTime.ToString("yyyyMMdd-HH:mm:ss> ")
        Else
            sDateTime = ""
        End If
        listStatus.BeginInit()
        listStatus.Items.Add(sDateTime + s)
        listStatus.ScrollIntoView(listStatus.Items.Count - 1)
        listStatus.EndInit()
    End Sub

Open in new window

0
MeritorWabco
Asked:
MeritorWabco
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I found this guys post and was able to get it to work using the code below:
http://www.developingfor.net/wpf/fun-with-the-wpf-scrollviewer.html

*I'm a WPF noob, though, so I don't really understand it yet.  There might even be a better approach but I have no idea...
    Public Sub UpdateStatus(ByVal s As String)
        Dim currDateTime As DateTime = DateTime.Now
        Dim sDateTime As String

        If (s <> gsLine) Then
            sDateTime = currDateTime.ToString("yyyyMMdd-HH:mm:ss> ")
        Else
            sDateTime = ""
        End If

        listStatus.Items.Add(sDateTime + s)
        Dim sv As ScrollViewer = FindScroll(listStatus)
        If Not IsNothing(sv) Then
            sv.ScrollToEnd()
        End If
    End Sub

    Private Function FindScroll(ByVal lb As ListBox) As ScrollViewer
        Dim scroll_border As Border = VisualTreeHelper.GetChild(lb, 0)
        If TypeOf scroll_border Is Border Then
            Dim scroll As ScrollViewer = scroll_border.Child
            If TypeOf scroll Is ScrollViewer Then
                Return scroll
            End If
        End If
        Return Nothing
    End Function

Open in new window

0
 
MeritorWabcoAuthor Commented:
Yeah I am a WPF noob also :).  But i got it to work also.  I tried using the ScrollViewer several different ways and still couldn't get it to work. I guess I just don't know what I am doing and it is showing!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now