Wpf stop stop event when MessageBox appears?

I have an event PreviewMouseDownin a TreeView to determine if a user can select another element based on some logic. If the current item data has changed, a MessageBox will appear that asks the user if he wants to discard the changes. if the user presses YES, I set e.Handled = false;to enable the new selection. and if the user presses NO, I set e.Handled = true;to cancel the new selection.

The problem is that although I set e.Handled = false, the stop event and the select event do not occur in the TreeView. Does anyone have a solution for this?

Thanks in advance!

+5
source share
2 answers

Changing the focus in the message box cancels the mouse event, so it does not matter if it is processed or not. Since you know which item the user tried to select before displaying the message box, simply select this item programmatically if the user presses YES.

+3
source

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
+1

All Articles