【问题标题】:Get listView item number from selected item in wpf and c#从wpf和c#中的选定项目获取listView项目编号
【发布时间】:2014-11-19 15:54:46
【问题描述】:

我有一个图像列表视图,我会知道从左键单击鼠标选择了哪个图像。 我没有找到任何方法可以做到这一点,并且我在这段代码中被阻止了,因为无法将 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>   

【问题讨论】:

    标签: c# wpf image listview kinect


    【解决方案1】:

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

    【讨论】:

    • @luca 表示DataContext不是Image类型,所以ListViewItem和Image的关系在这里不清楚。我怀疑您是否有一些用于 ListView 的 itemtemplate,而 Image 只是其中的一个元素。因此,您应该发布更多代码(尤其是 XAML 代码)以使其清晰。
    • 我尝试过这种方法,但我得到了一个异常,因为图像为空(数据上下文是视图框而不是图像)
    • @luca 所以试试这个(selectedItem.DataContext as Viewbox).Child as Image
    • 完美我获得了来源(我会尝试将其用作列表的索引)但我有一个小问题。鼠标上移事件选择下面的指针图像而不是我点击的图像,当我点击图像时,列表滚动以便该方法返回图像名称附近
    • 也许Click 事件不是您应该处理的。当SelectedItemListView 上发生变化时,也许您想运行一些代码?
    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 2023-03-18
    • 2018-02-15
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多