WPF中ListView 用鼠标选了其中一行,怎么知道选的是第几行啊。

如题wpf中的~~求教... 如题

wpf中的~~求教
展开
 我来答
Yeshirow
推荐于2016-01-20 · TA获得超过340个赞
知道小有建树答主
回答量:74
采纳率:0%
帮助的人:139万
展开全部
刚好解决了此问题.
以下是 VB Source Code:

Imports System.Windows

Public Sub ListView_GetCurrentItemInfo( _
ByVal pListview As Controls.ListView, _
ByVal e As UIElement, _
Optional ByRef dwRowIndex As Integer = Nothing, _
Optional ByRef dwCellIndex As Integer = Nothing)

Dim rowIndex As Integer = -1, cellIndex As Integer = -1
Dim pCP As Controls.ContentPresenter
Dim xCP As DependencyObject

xCP = Media.VisualTreeHelper.GetParent(e)
If xCP Is Nothing Then GoTo FinalStep
pCP = TryCast(xCP, Controls.ContentPresenter)
Do While pCP Is Nothing
xCP = Media.VisualTreeHelper.GetParent(xCP)
pCP = TryCast(xCP,Controls.ContentPresenter)
Loop
rowIndex = pListview.Items.IndexOf(pCP.Content)
Dim pRP As Controls.GridViewRowPresenter
xCP = Media.VisualTreeHelper.GetParent(pCP)
If xCP Is Nothing Then GoTo FinalStep
pRP = TryCast(xCP, Controls.GridViewRowPresenter)
Do While pRP Is Nothing
xCP = Media.VisualTreeHelper.GetParent(xCP)
pRP = TryCast(xCP, Controls.GridViewRowPresenter)
Loop
For i As Integer = 0 To Media.VisualTreeHelper.GetChildrenCount(pRP) - 1
If Media.VisualTreeHelper.GetChild(pRP, i) Is pCP Then
cellIndex = i
GoTo FinalStep
End If
Next
FinalStep:
dwRowIndex = rowIndex
dwCellIndex = cellIndex
End Sub

dwRowIndex 返回的是行.
dwCellIndex 返回的是列.
上述代码当 ListView 的 Cell 为单个 Control 或者直接是 ContentPresenter 时通过测试, 其他情形未作测试.

下面是 ListView (x:Name=lv) 单击鼠标的事件:

Private Sub lvClick(ByVal sender As Object, ByVal e As Input.MouseEventArgs) Handles lv.MouseLeftButtonDown
Dim dwRow, dwCell As Integer
ListView.GetCurrentItemInfo(lv, DirectCast(e.OriginalSource, UIElement), dwRow, dwCell)
...
End Sub

-------------
如果需要 C# 的Code
可以参考一下这个文章:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a52c0d07-76e5-4e6a-8987-5f0eec637939
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式