Click here to Skip to main content
15,561,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!

I am very slowly building my first WPF application, so please be patient. I have been stuck with this one for a while now, so I hope someone here at CP can help me.

I have a data grid, when the user double clicks on it, i want to read a unique id that is stored in a column with a known index. Using this article:[^] I have written the following code, which works allright:

'find datagridcell the user double clicked
dep = DirectCast(e.OriginalSource, DependencyObject)
Do While Not dep Is Nothing
    dep = VisualTreeHelper.GetParent(dep)
    If TypeOf (dep) Is DataGridCell Then
        'find datagridcellspanel to which the datagridcell belongs
        Do While Not dep Is Nothing
            dep = VisualTreeHelper.GetParent(dep)
            If TypeOf (dep) Is DataGridCellsPanel Then
                'find cell in position 1 --> contains the id
                dgc = DirectCast(VisualTreeHelper.GetChild(dep, 1), DataGridCell)
                lId = CLng(DirectCast(dgc.Content, TextBlock).Text.ToString)
                MsgBox("id i'm looking for: " & lId.ToString)
                Exit Do
            End If
        Exit Do
    End If

Now, if the column containing the id is hidden, this code will not work... I understand that this is because I am using the Visual Tree... Should I use the logical tree instead? I have tried to, but the parent of the datagridcell is nothing.

So, any idea how to do this? I don't need code as much as an idea of what class should I use. C# code is welcome too.

TIA for your help
Updated 1-Mar-11 3:24am

1 solution

Does this[^] help? It gets the underlying data source data because of the null (Nothing) problems. It's in C#, unfortunately but you should be able to follow.
Share this answer
iris.frigole 1-Mar-11 8:36am    
I will have a look to the linked article later today and let you know... I am a newbie, so I can't tell without reading everything more than once! Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900