WPF解决ScrollViewer内部鼠标滚轮无法控制上下翻页

WPF解决ScrollViewer内部鼠标滚轮无法控制上下翻页

最近发现ScrollViewer使用时鼠标在内部内容中滚动不能使其上下翻,只有鼠标移到最右侧滚动条上才有用

于是查找到了这位大佬的解决方式,测试了一下解决了问题,记录一下原文地址

ScrollViewer:当内容超过指定长度和宽度后,会出现滚动条,且可以使用鼠标滚轮滚动控制

当ScrollViewer中包含ItemsControl时,因为ItemsControl自身也有滚动功能,所以未作任何处理时无法实现滚动控制

使用ListBox示例,下列代码无法实现滚动

<ScrollViewer Grid.Column="1">
                <StackPanel>
                    <ListBox Background="AliceBlue">
                        <ListBox.ItemsSource>
                            <x:Array Type="TextBox">
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                                <TextBox Text="00000"/>
                            </x:Array>
                        </ListBox.ItemsSource>
                    </ListBox>
                    <ListBox Height="50" Background="Green">
                        <ListBox.ItemsSource>
                            <x:Array Type="Rectangle">
                                <Rectangle Height="20" Fill="Red"/>
                                <Rectangle Height="20" Fill="Red"/>
                                <Rectangle Height="20" Fill="Orange"/>
                                <Rectangle Height="20" Fill="Red"/>
                                <Rectangle Height="20" Fill="Gray"/>
                                <Rectangle Height="20" Fill="Red"/>
                            </x:Array>
                        </ListBox.ItemsSource>
                    </ListBox>
                </StackPanel>
            </ScrollViewer>

解决方法:将ListBox的滚动事件与外层的ScrollViewer的滚动关联起来

public void UseTheScrollViewerScrolling(FrameworkElement fElement)
        {
            fElement.PreviewMouseWheel += (sender, e) =>
            {
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                fElement.RaiseEvent(eventArg);
            };
        }

当两者之间还有别的元素时,这个方法的参数应该传入的是ScrollViewer的直接子元素,而不是深层的ListBox
可实现滚动的代码如下:
xaml:

  <ScrollViewer Grid.Column="2">
                <Grid x:Name="grid">
                    <StackPanel >
                        <ListBox Background="AliceBlue">
                            <ListBox.ItemsSource>
                                <x:Array Type="TextBox">
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                    <TextBox Text="00000"/>
                                </x:Array>
                            </ListBox.ItemsSource>
                        </ListBox>
                        <ListBox Height="50" Background="Green">
                            <ListBox.ItemsSource>
                                <x:Array Type="Rectangle">
                                    <Rectangle Height="20" Fill="red"/>
                                    <Rectangle Height="20" Fill="red"/>
                                    <Rectangle Height="20" Fill="Orange"/>
                                    <Rectangle Height="20" Fill="red"/>
                                    <Rectangle Height="20" Fill="Gray"/>
                                    <Rectangle Height="20" Fill="red"/>
                                    <Rectangle Height="20" Fill="red"/>
                                </x:Array>
                            </ListBox.ItemsSource>
                        </ListBox>
                    </StackPanel>
                </Grid>
            </ScrollViewer>

C#:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UseTheScrollViewerScrolling(grid);
        }
        public void UseTheScrollViewerScrolling(FrameworkElement fElement)
        {
            fElement.PreviewMouseWheel += (sender, e) =>
            {
                var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                eventArg.Source = sender;
                fElement.RaiseEvent(eventArg);
            };
        }
    }

你可能感兴趣的:(wpf,计算机外设,c#)