【问题标题】:How can i get the row and column index of a cell in a listview?如何获取列表视图中单元格的行和列索引?
【发布时间】:2019-12-10 04:34:36
【问题描述】:

我有一个 WPF 应用程序,其列表视图由 UI 中的网格视图表示。我想要一个事件,当单击此列表视图中的单元格时,我可以看到单元格的行和列索引。目前我可以获取整个列表项(行),但不能获取被点击的列。

例如,我的列表视图如下所示: List view example

当点击电子邮件地址 sammy.doe@gmail.com 时,row=2 和 column=2 应该在事件处理程序中可用,当点击年龄 39 时,row=1 和 column=1 应该可用。

如何在单击事件处理程序中获取此信息。

提前致谢。

【问题讨论】:

    标签: wpf vb.net listview gridview


    【解决方案1】:

    在解决这个问题几天后,我找到了一个解决方案,我在点击事件上使用鼠标点位置。然后在 Gridview 列中搜索,通过考虑指针的 X 位置来找到它所在的列。

    下面是我的代码:

    Class MainWindow
        Public Sub New()
    
            InitializeComponent()
            Dim items As List(Of User) = New List(Of User)
            items.Add(New User() With {.Name = "Isaac", .Age = 23, .Email = "isaac@email.co.uk", .City = "London", .Phone = "123456789"})
            items.Add(New User() With {.Name = "Jacob", .Age = 29, .Email = "jacob@email.co.uk", .City = "Royston", .Phone = "214563255"})
            lv.ItemsSource = items
    
            Me.DataContext = Me
        End Sub
    
    
        Private Sub Lv_PreviewMouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
            Dim gv = DirectCast(lv.View, GridView)
            Dim lvi = DirectCast(sender, ListViewItem)
            Dim p = e.GetPosition(lvi)
            Dim c_n = GetColumnNumber(gv, p)
            Debug.Print("Row: " + lv.SelectedIndex.ToString + "     Column: " + c_n.ToString)
        End Sub
    
        Public Function GetColumnNumber(gv As GridView, p As Point) As Integer
            Dim n As Integer = 0
            Dim columnFound As Boolean = False
            Dim leftSide = 0
            Dim rightSide = 0
            For Each c In gv.Columns
                rightSide += c.ActualWidth
                If InRange(leftSide, rightSide, p.X) Then
                    columnFound = True
                    Exit For
                End If
                leftSide = rightSide
                n += 1
            Next
            If Not columnFound Then n = Nothing
            Return n
        End Function
    
        Public Function InRange(lower As Double, upper As Double, value As Double) As Boolean
            If value < upper And value > lower Then Return True
            Return False
        End Function
    End Class
    
    
    
    Public Class User
        Public Property Name As String
        Public Property Age As Integer
        Public Property Email As String
        Public Property Phone As String
        Public Property City As String
    End Class
    

    这是我的 Xaml:

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ListViewSelection"
            mc:Ignorable="d"
            Title="List View Selection" Height="450" Width="800">
        <Grid>
            <ListView Margin="10" Name="lv">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <EventSetter Event="PreviewMouseLeftButtonUp" Handler="lv_PreviewMouseLeftButtonUp"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Name}"/>
                        <GridViewColumn Header="Age" Width="auto" DisplayMemberBinding="{Binding Age}"/>
                        <GridViewColumn Header="Email" Width="auto" DisplayMemberBinding="{Binding Email}"/>
                        <GridViewColumn Header="Phone" Width="auto" DisplayMemberBinding="{Binding Phone}"/>
                        <GridViewColumn Header="City" Width="auto" DisplayMemberBinding="{Binding City}"/>
                    </GridView>
                </ListView.View>
    
            </ListView>
    
        </Grid>
    </Window>
    

    我希望这可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多