Mouse scroll not working in a scroll viewer with a wpf datagrid and additional UI elements

前端 未结 10 1655
醉话见心
醉话见心 2021-02-03 18:41

I am trying to figure out how to get the mouse scroll working on a wpf window with a scrollviewer and a datagrid within it. The WPF and C# code is below



        
10条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-03 18:42

    I'm assuming the DataGrid does not need to scroll - Set the VerticalScrollBar="None" on the DataGrid.

    The DataGrid swallows the mouse scroll event.

    What I found is to use the PreviewMouseWheel event to scroll the container that you want to scroll. You will need to name the scrollviewer to change the Vertical offset.

    private void DataGrid_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
           scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset-e.Delta);
        }
    

提交回复
热议问题