Solved

WPF Listbox Scroll to Bottom VB.Net

Posted on 2010-11-08
2
1,039 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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