Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么listviewitems不出现在命中测试结果中?_C#_Wpf_Xaml - Fatal编程技术网

C# 为什么listviewitems不出现在命中测试结果中?

C# 为什么listviewitems不出现在命中测试结果中?,c#,wpf,xaml,C#,Wpf,Xaml,所以我在这里举一个例子: 以测试listview中的项目。但是,它给了我controltemplate中的项,而没有给我实际的listview项。我不确定为什么会发生这种情况,也不知道如何使鼠标在listviewitems上进行点击测试 hitResultsList.Clear(); Point pt = e.GetPosition((UIElement)sender); // Perform the hit test against a given portion of the

所以我在这里举一个例子:

以测试listview中的项目。但是,它给了我controltemplate中的项,而没有给我实际的listview项。我不确定为什么会发生这种情况,也不知道如何使鼠标在listviewitems上进行点击测试

  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)
    );
它从my controltemplate返回边框scrollviewer和网格,但不返回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>

如您所述,您当前的命中测试返回
网格或
滚动查看器
,您可以使用该属性定位
列表视图项
。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
    }
}

正如您提到的,您需要确定鼠标是否位于
ListViewItem
上,为什么不使用dependency属性?这在拖放操作期间不起作用