Wpf stop routing event when MessageBox appear?

一世执手 提交于 2019-12-07 06:13:21

问题


I've PreviewMouseDown event on TreeView in order to determine if user can select other item based on some logic. If the current item data changed, will appear MessageBox that asks the user if he want to discard the changes. if user press YES , I set e.Handled = false; to enable the new selection. and if user press NO, I set e.Handled = true; to cancel the new selection.

The problem is that although I set e.Handled = false , the event stop and no selection event occurs on TreeView. Someone has solution for that?

Thanks in advance!


回答1:


The focus change to the message box cancels the mouse down event so it doesn't matter whether it is handled or not. Since you know which item the user was trying to select before you displayed the message box, simply select that item programmatically if the user presses YES.




回答2:


I realize this is an old question, bu thought I would add my answer.

Actually, @yossharel, you DO know which item the user was trying to select, from the MouseEventArgs. You need to look at e.OriginalSource (probably a TextBlock), which is what the user clicked on. As such, it has a DataContext.

So, set the TreeView's SelectedItem equal to e.OriginalSource.DataContext.

In VB, you can be explicit or implicit: myTreeView.SelectedItem = CType(e.OriginalSource, TextBlock).DataContext() myTreeView.SelectedItem = e.OriginalSource.DataContext()

In C#, you will need to determine the type of e.OriginalSource. Do this by putting in a break point, and see what Studio tells you that it is. In this example: myTreeView.SelectedItem = ((TextBlock)e.OriginalSource).DataContext()

Here is an example from my own code. In my case, it's a DataGrid instead of a TreeView, but should work the same. I use this code to prompt the user if there are unsaved changes on the selected item. If the user answers "Yes" to "Continue without saving?" the code continues with the new selection. Otherwise, I let the Message Box block the RoutedEvent, preventing the SelectionChanged event from firing.

    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


来源:https://stackoverflow.com/questions/4556422/wpf-stop-routing-event-when-messagebox-appear

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!