Solved

WPF Listbox Scroll to Bottom VB.Net

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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