2016-10-07 11 views
0

UWPの特定のポイントですべてのUIElementを取得するにはどうすればよいですか?ユーザーがPointerReleasedイベントでポインタを解放すると、すべてのUIElementsを取得したいと考えています。 VisualTreeHelper.FindElementsInHostCoordinatesメソッドを使用しましたが、null値が返されます。特定のポイントのすべてのUIElementを取得する

CSのFILE:

private void drawingPanel_PointerReleased(object sender, PointerRoutedEventArgs e) 

      PointerPoint endPoint = e.GetCurrentPoint(this.drawingPanel);    
      List<UIElement> list = VisualTreeHelper.FindElementsInHostCoordinates(endPoint.Position, this) as List<UIElement>;    

     } 

UI XAML:お使いの助けを

<UserControl ***** bla bla *****>  

     <UserControl.Resources> 


      <DataTemplate x:Key="ImageTemplate"> 
       <Viewbox Stretch="UniformToFill" StretchDirection="Both" > 
        <Grid> 
         <ScrollViewer MinZoomFactor="1" MaxZoomFactor="3" ZoomMode="Disabled" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> 
          <Image Source="{Binding BlockData}" Height="{Binding ScreenHeight, Mode=TwoWay}" Width="{Binding ScreenWidth, Mode=TwoWay}" 
            Holding="Image_Holding" 
            PointerPressed="Image_PointerPressed" /> 
         </ScrollViewer> 
        </Grid> 
       </Viewbox> 
      </DataTemplate> 


      <DataTemplate x:Key="TextTemplate" ScrollViewer.VerticalScrollMode="Disabled" ScrollViewer.HorizontalScrollMode="Disabled"> 

       <Viewbox Stretch="Uniform" StretchDirection="Both" VerticalAlignment="Top" Grid.Row="1"> 
        <RichEditBox Width="{Binding ScreenWidth, Mode=TwoWay}" Name="richEditor" l:RtfText.RichText="{Binding BlockData}" Margin="0" BorderThickness="0" IsHitTestVisible="True" IsTapEnabled="True" GotFocus="richEditor_GotFocus" > 
        </RichEditBox> 
       </Viewbox> 

      </DataTemplate> 
      <DataTemplate x:Key="GapTemplate"> 
       <Grid Height="20" > 
       </Grid> 
      </DataTemplate> 

      <l:NoteTypeTemplateSelector x:Key="NoteTypeTemplateSelector" 
      TextTemplate="{StaticResource TextTemplate}" 
      ImageTemplate="{StaticResource ImageTemplate}" 
      GapTemplate="{StaticResource GapTemplate}"> 
      </l:NoteTypeTemplateSelector> 

     </UserControl.Resources> 

     <Grid Name="ContainerGrid" Background="White" PointerPressed="ContainerGrid_PointerPressed" > 
      <Grid.RowDefinitions> 

       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="*"></RowDefinition> 
       <RowDefinition Height="30"></RowDefinition> 
      </Grid.RowDefinitions> 
      <ListView x:Name="NoteList" Background="Transparent" 
         Grid.Row="1"      
         Style="{StaticResource ListViewStyle1}" 
         ItemsSource="{Binding Data}"     
         ItemTemplateSelector="{StaticResource NoteTypeTemplateSelector}"> 
       <ListView.ItemContainerStyle> 
        <Style TargetType="ListViewItem"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <ContentPresenter/> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </ListView.ItemContainerStyle> 
      </ListView> 
      <SwapChainPanel Name="DxPanel" Grid.Row="1" Visibility="Collapsed"></SwapChainPanel> 
      <c:DrawingControl x:Name="drawingPanel" Grid.Row="1" 
           Holding="drawingPanel_Holding" 
           PointerPressed="drawingPanel_PointerPressed" 
           PointerMoved="drawingPanel_PointerMoved" 
           PointerReleased="drawingPanel_PointerReleased" 
           PointerWheelChanged="drawingPanel_PointerWheelChanged" 
           PointerExited="drawingPanel_PointerExited" 

           ManipulationStarted="drawingPanel_ManipulationStarted" 
           ManipulationDelta="drawingPanel_ManipulationDelta" 
           ManipulationCompleted="drawingPanel_ManipulationCompleted"></c:DrawingControl> 
      <c:ReadModeToolbar x:Name="ReadModeToolbar" Grid.Row="0"></c:ReadModeToolbar> 
      <c:EditModeToolbar x:Name="EditModeToolbar" Grid.Row="0" Visibility="Collapsed"></c:EditModeToolbar> 
      <TextBlock Grid.Row="2" x:Name="InfoText" Text="Offset"></TextBlock> 
     </Grid> 
    </UserControl> 

感謝。

答えて

1

VisualTreeHelper.FindElementsInHostCoordinatesは、特定のポイントにあるすべての要素の列挙を返します。

PointerReleasedイベントには、ポインタの位置がPointerRoutedEventArgsに含まれています。 GetCurrentPoint()メソッド。

GetCurrentPointとFindElementsInHostCoordinatesに同じUIElement(ページの可能性が高い)を渡すと、座標系が一致します。

+0

ありがとうございます。このメソッドを試しましたが、null値が返されます。私はコードを見ることができる私の質問を編集しています。 –

+0

同じ座標を使用する必要があります。描画パネルを基準にポイントを取得してから、これを基準にします。座標が一致するように両方の場所で同じ要素を使用します。 –

+0

私はそれを試みましたが、結果は同じです、このメソッドは常に空を返します。私はすでにそのようにしてみました:PointerPoint p = e.GetCurrentPoint(this.testGrid); リストとしてリストVisualTreeHelper.FindElementsInHostCoordinates(p.RawPosition、this.testGrid); –

関連する問題