Solved

WPF Listbox Scroll to Bottom VB.Net

Posted on 2010-11-08
2
991 Views
Last Modified: 2012-05-10
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
Comment
Question by:MeritorWabco
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 34087492
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
 

Author Comment

by:MeritorWabco
ID: 34087698
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now