Private Sub DGrid_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)
' iteratively traverse the visual tree
While (dep IsNot Nothing) AndAlso Not (TypeOf dep Is DataGridRow OrElse TypeOf dep Is Primitives.DataGridColumnHeader OrElse TypeOf dep Is DataGrid)
dep = VisualTreeHelper.GetParent(dep)
End While
If dep Is Nothing Then
Exit Sub
End If
If TypeOf dep Is Primitives.DataGridColumnHeader Then
Dim CurrentHeader As Primitives.DataGridColumnHeader = TryCast(dep, Primitives.DataGridColumnHeader)
If Not CurrentHeader Is Nothing Then
CurrentHeader.ContextMenu.Items.Add("Option")
End If
ElseIf TypeOf dep Is DataGridRow Then
'Something else
End If
End Sub