应用场景:
在WPF应用程序中,当在ScrollViewer中包含Listview时,在Listview上的鼠标滚动事件无效。
实例解决方法:
xaml代码:
<Window x:Class="ScrollViewOnListView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ScrollViewer Name="instScroll" Loaded="instScroll_Loaded" >
<StackPanel Name="instSP" >
<Button Height="100">
</Button>
<Button Height="200">
</Button>
<Button Height="200">
</Button>
<ListView Name="listview" >
<ListViewItem Height="30">
</ListViewItem>
<ListViewItem Height="30">
</ListViewItem>
</ListView>
<Button Height="100">
</Button>
<Button Height="200">
</Button>
<Button Height="200">
</Button>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ScrollViewOnListView
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void instScroll_Loaded(object sender, RoutedEventArgs e)
{
listview.AddHandler(MouseWheelEvent,new RoutedEventHandler(MyMouseWheelH),true);
}
private void MyMouseWheelH(object sender, RoutedEventArgs e)
{
MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;
double x = (double)eargs.Delta;
double y = instScroll.VerticalOffset;
instScroll.ScrollToVerticalOffset(y - x);
}
}
}