2017-01-16 66 views
1

我有一个网格,其中的内容添加到listview.I已添加另一个网格,这是一个叠加在列表视图上。我需要滚动底层网格中的项目。如何检测事件的下层

<Grid> 
     <ListView> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
      <TextBlock Text="CONTENT"/> 
     </ListView> 
     <Grid Background="Transparent" ManipulationDelta="Grid_ManipulationDelta" ManipulationMode="All"/> 
    </Grid> 
+1

请在此链接后面提供示例(http://stackoverflow.com/help/mcve) – peval27

+0

如果您从叠加网格中删除了背景=“透明”或设置了“IsHitTestVisible =”False “',鼠标事件应该到达'ListView'。如果这样做不起作用或者覆盖网格包含更多内容,则可能必须捕获鼠标事件并将它们明确地转发到列表视图。 – grek40

+0

@ grek40我需要操作覆盖网格事件以及滚动列表视图中的项目。建议我请 – shalusri

回答

0

您可以捕获鼠标事件(或更好的PreviewMouseWheel事件),并明确地再提高它为ListView:

<Grid> 
    <ListView x:Name="listview1"> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
     <TextBlock Text="CONTENT"/> 
    </ListView> 
    <Grid Background="Transparent" PreviewMouseWheel="Grid_PreviewMouseWheel"/> 
</Grid> 

代码将事件转发:

private void Grid_PreviewMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    if (!e.Handled) 
    { 
     Decorator border = VisualTreeHelper.GetChild(listview1, 0) as Decorator; 
     ScrollViewer sv = border.Child as ScrollViewer; 
     var arg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta) { RoutedEvent = UIElement.MouseWheelEvent, Source = sender }; 
     sv.RaiseEvent(arg); 
    } 
} 

注意我只是用第一种方式找到ScrollViewer内部的,可能会有更好/更多的保存/ ...方式来做到这一点。

(可选)如果您不需要覆盖层上的鼠标滚轮事件,则可以选择标记e.Handled = true;

+0

请建议我触摸windows10的事件 – shalusri

+0

@shalusri对不起,我既不使用Windows 10也不使用触摸设备。但是,如果您在转发“MouseWheelEvent”时以类似的方式转发触发事件'PreviewTouchDown'' PreviewTouchUp'' PreviewTouchMove',它应该可以工作。只要记住捕获'预览[SomeEvent]'并提高'[SomeEvent]'。 – grek40