[英]Get listView item number from selected item in wpf and c#
I have a ListView of images and i would know which image is selected from left click mouse. 我有一个图像的ListView,我会知道从左键单击鼠标中选择了哪个图像。 I don't find any way to do this and i'm blocked on this code where it's impossible cast ListViewItem to Image for obtain index in list. 我找不到任何方法来执行此操作,并且我在此代码上受阻,无法将ListViewItem强制转换为Image以获取列表中的索引。
c#: C#:
private void listView_Click(object sender, MouseButtonEventArgs e)
{
var hitTestResult = VisualTreeHelper.HitTest(listViewExercise, e.GetPosition(null));
var selectedItem = hitTestResult.VisualHit;
while (selectedItem != null)
{
if (selectedItem is System.Windows.Controls.ListViewItem)
{
break;
}
selectedItem = VisualTreeHelper.GetParent(selectedItem);
}
Image image = (Image)selectedItem;
Console.WriteLine(image.Source);
}
XAML: XAML:
<k:KinectRegion x:Name="ChoiceExercise" Background="Black" >
<DockPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="5*"/>
</Grid.RowDefinitions>
<k:KinectUserViewer Grid.Row="0" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<ScrollViewer k:KinectRegion.IsHorizontalRailEnabled="True" k:KinectRegion.IsScrollInertiaEnabled="true" VerticalScrollBarVisibility="Disabled" Grid.Row="1" >
<ListView Grid.Row="1">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
<Viewbox Width="300" >
<Image Stretch="UniformToFill" Source="Images/la.jpg"/>
</Viewbox>
<Viewbox Width="300">
<Image Stretch="UniformToFill" Source="Images/end exercise win.jpg"/>
</Viewbox>
</ListView>
</ScrollViewer>
</Grid>
</DockPanel>
</k:KinectRegion>
There is an easier way to obtain the item container, and once you do that, you just need to extract the DataContext
to get the item from the ItemsSource
: 有一种获取项目容器的简便方法,完成此操作后,只需提取DataContext
即可从ItemsSource
获取项目:
private void listView_Click(object sender, MouseButtonEventArgs e)
{
var source = e.OriginalSource as DependencyObject;
if (source == null)
return;
var selectedItem = ItemsControl.ContainerFromElement((ItemsControl)sender, source)
as FrameworkElement;
if (selectedItem == null)
return;
var image = selectedItem.DataContext as Image;
if (image != null)
Console.WriteLine(image.Source);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.