Key stroke to scroll horizontally in listbox

Feb 19, 2014 at 1:22 PM
Hello,
I'd like to map keys to scroll the listbox horizontally. I know the arrow keys can be used, but I'd like to have other keys available, as well.
I have keys mapped to scroll vertically (using the SelectedIndex property).
-On_KeyDown {
                        if($_.Key -eq "j") { 
                            $LB.SelectedIndex += 1
                        }
                        if($_.Key -eq "k") { 
                            $c = $LB.SelectedIndex
                            if ($c -gt 0) {
                                $LB.SelectedIndex -= 1
                            }
                        }
                        if($_.Key -eq "l") { 
                            # scroll, pushing text to left
                        }
                        if($_.Key -eq "h") { 
                            # scroll, pushing text to right
                        }
                    }
Thanks for any tips...
Coordinator
Feb 21, 2014 at 5:39 AM
Edited Feb 21, 2014 at 5:40 AM
It's a bit complicated to scroll a listbox, because you have to FIND the ScrollViewer that is in the ListBox's template....

http://stackoverflow.com/questions/4935603/wpf-how-to-scroll-a-listview-control-horizontal
http://stackoverflow.com/questions/10293236/accessing-the-scrollviewer-of-a-listbox-from-c-sharp

Once you find it, all you have to do is call .LineRight() or .LineLeft() (or PageRight/PageLeft).
-On_Loaded {
   # root node
   $Border = [System.Windows.Media.VisualTreeHelper]::GetChild($this, 0)
   $ScrollViewer = [System.Windows.Media.VisualTreeHelper]::GetChild($Border, 0)

   $this | Add-Member NoteProperty ScrollViewer $ScrollViewer
} -On_KeyDown {
   switch($_.Key){
      "H" {
         $this.ScrollViewer.LineRight()
      }
      "L" {
         $this.ScrollViewer.LineLeft()
      }
   }
}
It's probably a dumb idea to call the property "ScrollViewer" but whatever ... that code works for me ;-)
Marked as answer by ccbVT on 2/24/2014 at 6:39 AM
Feb 24, 2014 at 1:08 PM
Edited Feb 24, 2014 at 1:39 PM
Thanks for the reply,finally got it working.