问题描述
所以我按照这里的例子:http://msdn.microsoft.com/en-us/library/ms752097%28v=vs.110%29.aspx
so I'm following the example here: http://msdn.microsoft.com/en-us/library/ms752097%28v=vs.110%29.aspx
为了对列表视图中的项目进行测试.但是,它给了我控制模板中的项目,而没有给我实际的列表视图项目.我不确定为什么会发生这种情况,或者我不确定如何让它命中测试鼠标是否在 listviewitems 上.
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>
推荐答案
正如您所说,您当前的命中测试返回 Grid
或 ScrollViewer
,您可以使用FrameworkElement.TemplatedParent
属性来定位 ListViewItem
.即
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
}
}
这篇关于为什么listviewitems没有出现在命中测试结果中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!