簡體   English   中英

從WPF和C#中的選定項目中獲取listView項目編號

[英]Get listView item number from selected item in wpf and c#

我有一個圖像的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>   

有一種獲取項目容器的簡便方法,完成此操作后,只需提取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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM