James Joshua Street
James Joshua Street

Reputation: 3409

Why do listviewitems not appear in hit test results?

so I'm following the example here: http://msdn.microsoft.com/en-us/library/ms752097%28v=vs.110%29.aspx

in order to hittest the items in a listview. However, it gives me the items in the controltemplate without giving me the actual listview items. I am unsure why that happens or how I can make it hit test whether the mouse is over the listviewitems or not.

  hitResultsList.Clear();
  Point pt = e.GetPosition((UIElement)sender);

  // Perform the hit test against a given portion of the visual object tree.
  VisualTreeHelper.HitTest(canv, null, new HitTestResultCallback(MyHitTestResult),
    new PointHitTestParameters(pt)
    );

It returns the border scrollviewer and grid from my controltemplate, but not the actual items that are in the scrollviewer.

  <ControlTemplate>
    <Grid 
      Background="{TemplateBinding Background}"
      >
      <Grid.RowDefinitions>
        <RowDefinition 
          Height="{Binding GraphHeight, Source={x:Static DaedalusGraphViewer:SettingsManager.AppSettings},
                  Converter={StaticResource GridLengthConverter}}"
          />
        <RowDefinition Height="*"/>
        <RowDefinition Height="18" />
      </Grid.RowDefinitions>
      <Border
        Grid.ZIndex="1"                    
        Grid.Row="0"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}"
        >
        <Grid>
          <TextBlock 
            Foreground="{TemplateBinding Foreground}"
            VerticalAlignment="Center"
            HorizontalAlignment="Center"
            Text="Signal Names"
            />
        </Grid>
      </Border>
      <Canvas>
        <Line 
          Grid.ZIndex="2"
          x:Name="SelectedItemUnderline"
          Stroke="Black"
          StrokeThickness="3"
          Visibility="Collapsed"
          />
      </Canvas>
      <ScrollViewer 
        Grid.ZIndex="1"                    
        x:Name="SignalNameScrollViewer" 
        Grid.Row="1" Grid.RowSpan="2" 
        CanContentScroll="False"
        VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Visible" 
        >
        <ItemsPresenter />
      </ScrollViewer>
    </Grid>
  </ControlTemplate>

Upvotes: 0

Views: 517

Answers (1)

Lukazoid
Lukazoid

Reputation: 19416

As you have stated your current hit-testing returns either the Grid or ScrollViewer you could use the FrameworkElement.TemplatedParent property to locate the ListViewItem. i.e.

HitTestResult hitTestResult; // TODO either from callback or result
var fe = hitTestResult.VisualHit as FrameworkElement;
if(fe != null)
{
    var listViewItem = fe.TemplatedParent as ListViewItem;
    if(listViewItem != null)
    {
        // TODO Do something with the ListViewItem
    }
}

Upvotes: 2

Related Questions