简体   繁体   中英

Getting all UIElements in a specific Point

How can I get all UIElements at a specific point in UWP? I want to get all UIElements when the user release the pointer (in PointerReleased event). I have used VisualTreeHelper.FindElementsInHostCoordinates method but it returns null value.

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>

Thanks for your help.

VisualTreeHelper.FindElementsInHostCoordinates will return an enumeration of all of the elements at a specific point.

The PointerReleased event includes the pointer position in its PointerRoutedEventArgs . GetCurrentPoint () method.

Pass the same UIElement (likely your Page) to GetCurrentPoint and FindElementsInHostCoordinates so the coordinate systems will match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM