I understand that this is an old question, I thought I would add my answer.
Actually, @yossharel, you know which element the user was trying to select from MouseEventArgs. You need to look at the e.OriginalSource (possibly TextBlock) that the user clicked on. So it has a DataContext.
So set TreeView SelectedItem to e.OriginalSource.DataContext.
VB : myTreeView.SelectedItem = CType (e.OriginalSource, TextBlock).DataContext() myTreeView.SelectedItem = e.OriginalSource.DataContext()
# e.OriginalSource. , , Studio , . : myTreeView.SelectedItem = ((TextBlock) e.OriginalSource).DataContext()
. DataGrid TreeView, . . "" " ?" . Message Box RoutedEvent, SelectionChanged.
Private Sub dgDataGrid_PreviewMouseLeftButtonDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs) Handles dgDataGrid.PreviewMouseLeftButtonDown
If dgDataGrid.SelectedItem IsNot Nothing Then
If MyDataContext.ExternalViewModel.ItemIsModified Then
Dim prompt As String = String.Format("Changes have not been saved.{0}{0}Continue without saving?", vbCrLf)
Dim title As String = "Changes Not Saved"
Dim result As MsgBoxResult = MsgBox(prompt, MsgBoxStyle.Exclamation Or MsgBoxStyle.YesNo, title)
If result = MsgBoxResult.Yes Then
dgDataGrid.SelectedItem = e.OriginalSource.DataContext()
End If
End If
End If
End Sub
Private Sub dgDataGrid_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles dgDataGrid.SelectionChanged
MyDataContext.SetSearchItem(dgDataGrid.SelectedItem)
End Sub