C# 从wpf和c中的选定项目获取listView项目编号#

C# 从wpf和c中的选定项目获取listView项目编号#,c#,wpf,image,listview,kinect,C#,Wpf,Image,Listview,Kinect,我有一个图像列表视图,我会知道哪个图像是从鼠标左键选择的。 我找不到任何方法来实现这一点,我被这段代码阻塞了,因为无法将ListViewItem转换为图像以获取列表中的索引 c#: XAML: 有一种更简单的方法来获取项目容器,一旦您这样做,您只需要提取数据上下文,从项目资源获取项目: private void listView_Click(object sender, MouseButtonEventArgs e) { var source = e.OriginalSource as

我有一个图像列表视图,我会知道哪个图像是从鼠标左键选择的。 我找不到任何方法来实现这一点,我被这段代码阻塞了,因为无法将ListViewItem转换为图像以获取列表中的索引

c#:

XAML:


有一种更简单的方法来获取项目容器,一旦您这样做,您只需要提取
数据上下文
,从
项目资源
获取项目:

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);
}

@这意味着DataContext不是Image类型,因此ListViewItem和Image之间的关系在这里不清楚。我怀疑您是否有ListView的itemtemplate,而图像只是其中的一个元素。所以你应该发布更多的代码(特别是XAML代码)来澄清。我尝试过这个方法,但我得到了一个异常,因为image为null(datacontext为viewbox而不是image)@luca所以试试这个
(selectedItem.datacontext为viewbox)。Child为image
完美我获得了源代码(我将尝试使用它作为列表的索引)但我有个小问题。鼠标向上移动事件选择指针下面的图像,而不是我单击过的图像,当我单击图像时,列表滚动以使方法返回图像名称附近可能
单击
事件不是您应该处理的。当
列表视图上的
选择editem
更改时,您可能想运行一些代码?
<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>   
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);
}