Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll a ListBox programmatically the same way the MouseWheelEvent does?

Tags:

c#

wpf

Ok, I have the following problem:

I would like to scroll an overflowing ListBox up and down.

I would like to do it programatically in a custom control inheriting from ListBox. I've seen and tested things such as scrollIntoView. However I would like to have a scrolling similar to what you can have when using the mouse's wheel.

I don't want to have the mouse involved at all (I'm developing for the Kinect, and since there are 2 cursors, I don't want to use the Mouse event args)

a google search didn't turn up much: I've read plenty of thread on how to scroll in code behind using scrollIntoView, or putting a scrollbar and such.

like image 612
astreal Avatar asked Aug 30 '25 18:08

astreal


1 Answers

I think this will involve two steps:

  1. Find the scrollViewer control inside listBox template
  2. Perform the actual scrolling in that scrollViewer

For the first step implementation please take a look here. Here is the code snippet extracted from there:

this.Loaded += MainWindow_Loaded;

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    var scrollViewer = listbox.Template.FindName("Scroller", listbox);
}

And for the second step you should use one of the methods from here, LineDown or PageDown probably.

P.S.: I haven't tested this approach at all since I do not have VS installed so feel free to add the needed code here.

like image 61
Snowbear Avatar answered Sep 02 '25 06:09

Snowbear