Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Item by item scrolling in a WPF Listview

Tags:

c#

wpf

xaml

I have a listview which is quite small in height, but has 3-4 listviewitems which takes up the whole size of the Listview (so only one item can be displayed at once)

And if the user scrolls on it, the listview doesn't scroll 1 item at a time, it scrolls 2 items at a time (single scroll)

How would you set it so 1 scroll = one item down/up?

Hope I made myself clear with this, if not just tell me.

like image 761
Tokfrans Avatar asked Apr 17 '14 22:04

Tokfrans


1 Answers

I assume you're talking about the MouseWheel scroll here.

The MouseWheel scroll really depends on the IScrollInfo implementation. I suggest you to handle the MouseWheel event yourself before the ScrollViewer does. So basically, you could do something like following:

Handle the PreviewMouseWheel event on ListBox

<ListBox PreviewMouseWheel="ListBox_PreviewMouseWheel" Height="108" Width="100" x:Name="list" >
    <Button Content="Button 1" Height="100"/>
    <Button Content="Button 2" Height="100"/>
    <Button Content="Button 3" Height="100"/>
    <Button Content="Button 4" Height="100"/>
    <Button Content="Button 5" Height="100"/>
    <Button Content="Button 6" Height="100"/>
    <Button Content="Button 7" Height="100"/>
    <Button Content="Button 8" Height="100"/>
    <Button Content="Button 9" Height="100"/>
</ListBox>

In the code behind, fire the ScrollBar.LineDownCommand or ScrollBar.LineUpCommand when you scroll down or up.

private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
    {
        ScrollBar.LineDownCommand.Execute(null, e.OriginalSource as IInputElement);
    }
    if (e.Delta < 0)
    {
        ScrollBar.LineUpCommand.Execute(null, e.OriginalSource as IInputElement);
    }
    e.Handled = true;
}

Therefore, you turned the MouseWheel scroll into the LineDown/LineUp.

like image 125
terry Avatar answered Sep 21 '22 14:09

terry