2

之前我问过如何取消 WPF TreeViewItem.Selected 事件

回答者建议我在选择发生之前处理 PreviewMouseDown 事件。那讲得通。

我试过这样做...

XAML...

<TreeView Name="TreeViewThings"
    ...
    PreviewMouseDown="TreeViewThings_PreviewMouseDown"
    TreeViewItem.Expanded="TreeViewThings_Expanded"
    TreeViewItem.Selected="TreeViewThings_Selected" >

视觉基本...

子 TreeViewThings_PreviewMouseDown(...)
    如果 UnsavedChangesExist() 那么
        e.Handled = UserCancelled()
    别的
        e.Handled = False
    万一
结束子

函数 UnsavedChangesExist() 作为布尔值
    ...
结束功能

函数 UserCancelled() 作为布尔值
    Return MessageBox.Show("放弃未保存的更改?", _
                           “未保存的更改”,_
                           MessageBoxButton.OKCancel, _
                           MessageBoxImage.Question) = MessageBoxResult.Cancel
结束功能

这只是一种工作......

  • 如果没有未保存的更改,那么它会正常进行并执行 TreeViewThings_Selected()。

如果有未保存的更改,那么我会看到 MessageBox...

MessageBox:继续并放弃未保存的更改?确定/取消 http://img25.imageshack.us/img25/141/discard2yk0.gif

  • 如果我然后选择取消,它会取消。

  • 但是,如果我改为选择“确定”来放弃我未保存的更改,那么它无论如何都会取消——即使 e.Handled = False。它不会继续并执行 TreeViewThings_Selected()。

我认为有一个 MessageBox 以某种方式搞砸了这一事实。

我究竟做错了什么?

4

1 回答 1

1

The problem is that the messagebox causes your tree to lose focus. Have you tried setting the focus back to the tree after the messagebox is dismissed?

于 2010-09-17T18:14:10.183 回答