Solved

WPF Listbox Scroll to Bottom VB.Net

Posted on 2010-11-08
2
1,009 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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