2017-01-16 46 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