How do I access individual cell data in a WPF Datagrid from a custom control?

I have created a button column in my wpf form, but I need to use this button to access information in the datagrid.  How do I access the data in certain cells specifically if the data is autogenerated?  For example, if I wanted to access the data in column 4, row 2?
I can bind the button's context to some column data, then access the data via the sender object in the code.  But what if I want the button content to be something totally different?  Is there some other part of the button that I can bind this info to?  Am I going about this the wrong way?  Any help would be appreciated.
<my:DataGrid Name="DataGrid2" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" SelectionUnit="Cell">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn>
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>                                            <Button Content="{Binding Path=SomeColumnData}"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>


Private Sub myButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    'Do something with sender.content
End Sub

Open in new window

GravityPilotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GravityPilotAuthor Commented:
That should be button.content, not button.context.....sorry.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GravityPilotAuthor Commented:
Thanks CodeCruiser. I think I found what I needed from one of those links:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29

I modified Whitts code to get just what I needed.  Thanks to Whitts for that code as well!!

Private Sub myButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim depObj As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)
        'Get to DataGridCell level
        While (depObj IsNot Nothing) AndAlso Not (TypeOf depObj Is DataGridCell) 'AndAlso Not (TypeOf depObj Is Primitives.DataGridColumnHeader)
            depObj = VisualTreeHelper.GetParent(depObj)
        End While

        If depObj Is Nothing Then
            MsgBox("Can't Find Grid Info")
            Exit Sub
        End If

        If TypeOf depObj Is DataGridCell Then
            Dim cell As DataGridCell = TryCast(depObj, DataGridCell)

            'Get to DataGridRow level
            While (depObj IsNot Nothing) AndAlso Not (TypeOf depObj Is DataGridRow)
                depObj = VisualTreeHelper.GetParent(depObj)
            End While

            If depObj Is Nothing Then
                Exit Sub
            End If

            Dim row As DataGridRow = TryCast(depObj, DataGridRow)

            Dim columnIndex As Integer = cell.Column.DisplayIndex
            Dim rowIndex As Integer = FindRowIndex(row)
            Try
                MsgBox(ExtractBoundValue(row, cell, "EmployeeCompID#").ToString)
            Catch ex As Exception
                MsgBox("DataGrid Indexing Error")
            End Try
        End If
    End Sub

    'Determine the index of a DataGridRow
    Private Function FindRowIndex(ByVal row As DataGridRow) As Integer
        Dim dataGrid As DataGrid = TryCast(ItemsControl.ItemsControlFromItemContainer(row), DataGrid)

        Dim index As Integer = dataGrid.ItemContainerGenerator.IndexFromContainer(row)

        Return index
    End Function

    Private Function ExtractBoundValue(ByVal row As DataGridRow, ByVal cell As DataGridCell, ByVal columnname As String) As Object

        Try

            ' find the object that is realted to this row
            Dim data As Object = row.Item

            ' extract the property value
            Dim properties As ComponentModel.PropertyDescriptorCollection = ComponentModel.TypeDescriptor.GetProperties(data)
            Dim [property] As ComponentModel.PropertyDescriptor = properties(columnname)
            Dim value As Object = [property].GetValue(data)

            Return value
        Catch ex As Exception
            Return "DataGrid Extraction Failed"
        End Try
    End Function

Open in new window

0
GravityPilotAuthor Commented:
None of these links gave me exactly what I needed.  But they were enough to get me going in the right direction.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.