none
鼠标指针放在ListView控件上,滚动鼠标滚轮时,页面并不会滚动,这是为何? RRS feed

  • 问题

  • <Window x:Class="WpfApplication2.ScrollViewerTestForm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication2" mc:Ignorable="d" Title="ScrollViewerTestForm" Height="343" Width="704"> <ScrollViewer> <Grid> <Grid.RowDefinitions> <RowDefinition Height="400"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="200"></RowDefinition> </Grid.RowDefinitions> <Border Background="Beige"></Border> <Border Background="Red" Grid.Row="2"></Border> <ListView Grid.Row="1" Grid.ColumnSpan="4" BorderThickness="0" x:Name="AttachListView" ItemContainerStyle="{DynamicResource TodoListViewItem}"> <ListView.View> <GridView > <GridViewColumn Header="文件名" Width="200" DisplayMemberBinding="{Binding FileName}"></GridViewColumn> <GridViewColumn Header="上传者" Width="100" DisplayMemberBinding="{Binding Owner}"></GridViewColumn> <GridViewColumn Header="大小" Width="100" DisplayMemberBinding="{Binding Size}"></GridViewColumn> <GridViewColumn Header="最后修改" Width="120" DisplayMemberBinding="{Binding LastModified}"></GridViewColumn> </GridView> </ListView.View> </ListView> </Grid> </ScrollViewer> </Window>

    上面是完整的代码,鼠标放在上下两个Border的时候是可以滚动页面的,但是如果放在ListView上,此时滚轮失效了(即无论如何滚动滚轮,页面都不会动......)。

    要如何才可以解决这个问题?


    da jia hao!


    • 已编辑 liubin 2018年5月24日 6:47
    2018年5月24日 6:46

答案

  • Hi liubin,

    >>上面是完整的代码,鼠标放在上下两个Border的时候是可以滚动页面的,但是如果放在ListView上,此时滚轮失效了(即无论如何滚动滚轮,页面都不会动......)。

    要如何才可以解决这个问题?

    可能listview控件吞噬了鼠标滚轮事件, 一个解决方法是把listview的滚动事件传递给它的父控件,用这个事件完成PreviewMouseWheel="listview1_PreviewMouseWheel"

     <ScrollViewer>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="400" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="200" />
                </Grid.RowDefinitions>
                <Border Background="Beige" />
                <Border Grid.Row="2" Background="Red" />
                <ListView
                    x:Name="listview1"
                    Grid.Row="1"
                    Grid.ColumnSpan="4"
                    BorderThickness="0"
                    PreviewMouseWheel="listview1_PreviewMouseWheel"
                   ItemsSource="{Binding}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn
                                Width="200"
                                DisplayMemberBinding="{Binding FileName}"
                                Header="文件名" />
                            <GridViewColumn
                                Width="100"
                                DisplayMemberBinding="{Binding Owner}"
                                Header="上传者" />
                            <GridViewColumn
                                Width="100"
                                DisplayMemberBinding="{Binding Size}"
                                Header="大小" />
                            <GridViewColumn
                                Width="120"
                                DisplayMemberBinding="{Binding LastModified}"
                                Header="最后修改" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </ScrollViewer>

    C#代码

      private void listview1_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
            {
                if (!e.Handled)
                {
                    e.Handled = true;
                    var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
                    eventArg.RoutedEvent = UIElement.MouseWheelEvent;
                    eventArg.Source = sender;
                    var parent = ((Control)sender).Parent as UIElement;
                    parent.RaiseEvent(eventArg);
                }
            }

    顺便说一下, 您之前的这个帖子问题解决了吗, 如果解决的话, 请你把对您有帮助的回复标记为答案, 这会对其他遇到相同问题的社区人员有帮助, 谢谢您的理解。

    https://social.msdn.microsoft.com/Forums/zh-CN/51eb4895-909c-498c-af90-c7286e59345c/textboxplaceholder?forum=wpfzhchs#51eb4895-909c-498c-af90-c7286e59345c

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年5月25日 5:55
    版主