获取列表查看 wpf 和 c# 中选定项的项编号

本文关键字:编号 列表 wpf 获取 | 更新日期: 2023-09-27 18:31:05

我有一个图像列表视图,我会知道从左键鼠标中选择哪个图像。我找不到任何方法可以做到这一点,并且我被阻止了此代码,不可能将ListViewItem转换为图像以获取列表中的索引。

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:

<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>   

获取列表查看 wpf 和 c# 中选定项的项编号

有一种

更简单的方法来获取项目容器,一旦你这样做了,你只需要提取DataContextItemsSource中获取项目:

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