Item by item scrolling in a WPF Listview

前端 未结 1 1625
攒了一身酷
攒了一身酷 2021-01-12 21:52

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)

相关标签:
1条回答
  • 2021-01-12 22:19

    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.

    0 讨论(0)
提交回复
热议问题