Symptoms:
Consider a scenario where you have a VSTO 3.0 add-in for Excel 2007 that loads up a WPF control in a custom task pane as a context menu.
You have also subscribed to the MenuClick event for each item of the Context Menu. The context menu is displayed but some of the items of menu are displayed over the Excel sheet area as displayed in the below picture :-
If you click on the item which overlaps the Excel UI, you will notice that the menu click event is not fired for those items. The issue does not reproduce with Windows Form controls.
Cause:
This issue is a known issue reported with WPF.
Resolution:
One of the workarounds is to use the DispatcherFrame to pump messages and subscribe to GotFocusEvent and LostFocusEvent for the menu. (Similar to DoEvents method is VB6)
System.Windows.Threading.DispatcherFrame _frame;
_menu.AddHandler(System.Windows.UIElement.GotFocusEvent,new RoutedEventHandler(OnGotFocusEvent)); _menu.AddHandler(System.Windows.UIElement.LostFocusEvent, new RoutedEventHandler(OnLostFocusEvent));
Below is the implementation for the event procedures for the GotFocusEvent and LostFocusEvent
private void OnGotFocusEvent(object sender, RoutedEventArgs e) { if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, (DispatcherOperationCallback)delegate(object unused) { _frame = new DispatcherFrame(); Dispatcher.PushFrame(_frame); return null; }, null); } } private void OnLostFocusEvent(object sender, RoutedEventArgs e) { if (LogicalTreeHelper.GetParent((DependencyObject)e.OriginalSource) == _menu) { _frame.Continue = false; } }