C# WPF ListView如何在特定按钮上滚动

bcs8qyzn  于 2023-04-22  发布在  C#
关注(0)|答案(1)|浏览(187)

我实现水平滚动到我的ListView,它的工作,但我需要滚动水平与鼠标滚轮。
我到目前为止,我知道如何捕捉,如果鼠标滚轮是向上/向下滚动,但我只需要一个命令,将激活滚动左/右。

private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ListView listView= (ListView)sender;
         
            if (e.Delta < 0)
            {
                // Scrolls right, I need here to activate event that will scroll right,
                // something like listView.ScrollRightByOneItem()
            }
            else
            {
               // Scrolls left, I need here to activate event that will scroll left,
               // something like listView.ScrollLeftByOneItem()
            }
            e.Handled = true;
        }

XAML

<Grid>
        <ListView
            ItemsSource="{Binding Path=NotDeletedDevices}"
            MouseDoubleClick="ListView_MouseDoubleClick"
     PreviewMouseLeftButtonDown="ListView_PreviewMouseLeftButtonDown"
            PreviewMouseMove="ListView_PreviewMouseMove"
            SelectionMode="Single"
            PreviewMouseWheel="UIElement_OnPreviewMouseWheel"
            TouchDown="ListView_PreviewTouchDown"
            TouchMove="ListView_PreviewTouchMove">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>
  </Grid>

是否有任何事件会让我向右或向左滚动1个项目?
我需要一个命令/事件,以便在. xaml.cs文件中向右/向左滚动

gwbalxhn

gwbalxhn1#

我已经定义了一个名称Listview lview(我认为没有必要定义Scrollviewer,它的自动Auto我认为)

<ListView x:Name="lview" Grid.Column="0" Width="100" Height="100"
              ScrollViewer.HorizontalScrollBarVisibility="Auto" 
              ScrollViewer.VerticalScrollBarVisibility="Auto" 
              PreviewMouseWheel="ListViewer_PreviewMouseWheel"/>

由于滚动条属于Listviewer,因此必须找到它的示例,因此我定义了一个方法FindScrollViewer

private ScrollViewer FindScrollViewer(DependencyObject d)
    {
        if (d is ScrollViewer)
            return d as ScrollViewer;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(d); i++)
        {
            var sw = FindScrollViewer(VisualTreeHelper.GetChild(d, i));
            if (sw != null) return sw;
        }
        return null;
    }

    private void ListViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
    {
        //find the scrollViewer instance of the control
        var scrollViewer = FindScrollViewer(lview);

        //get the horizontal value
        var h = scrollViewer.HorizontalOffset;
        // check the delta of wheel mouse >0 or <0
        var d = e.Delta;
        var nv = 0d;
        // are you at the beginning or end the scrolling
        if (d < 0)
        {
            nv = d + h < 0 ? 0 : d + h;
        }
        else
        {
            nv = d + h > scrollViewer.ScrollableWidth ? scrollViewer.ScrollableWidth : d + h;
        }
        // to block the vertical move
        scrollViewer.ScrollToVerticalOffset(0);
        //set the new horizontal position
        scrollViewer.ScrollToHorizontalOffset(nv);
    }

我只是展示了当你想水平移动滚动条时如何阻止垂直移动。
您可以选择键盘(例如)在水平/垂直之间切换

相关问题