开发者

Wpf stop routing event when MessageBox appear?

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!


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜