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

时间:2014-11-19 15:54:46

标签: c# wpf image listview kinect

我有一个ListView图像,我知道从左键单击鼠标中选择了哪个图像。 我找不到任何方法来执行此操作,并且我在此代码中被阻止,因为无法将ListViewItem转换为Image以获取列表中的索引。

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>   

1 个答案:

答案 0 :(得分:2)

有一种更简单的方法来获取项目容器,一旦你这样做,你只需要提取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);
}