question

maverme avatar image
0 Votes"
maverme asked ·

Cant select items in a WPF combobox(VSTO Addin) if they are outside the parent window

When using a Combobox in a WPF UserControl via ElementHost for a VSTO AddIn, the Combobox items that are outside of the parent Office application cannot be selected.

In the clip below, the blue area is a WPF UserControl inside an ElementHost and you will see that '11111111' gets selected, but 'CCCCCCCC' does not because it is outside of the Outlook window. The second Combobox is just a plain WinForms Combobox and all items work fine, even outside the parent Outlook window.

76858-2021-03-11-14-45-23.gif

The Addin in the clip is just a super simple VSTO addin with a single CustomTaskPane, a WPF UserControl hosted inside an ElementHost(blue box), and then a plain WinForms Combobox.

I also tested creating a regular .Net WinForms Desktop application with this same WPF UserControl inside an ElementHost, and it works fine. So this leads me to believe its something with VSTO, but I have been unable to find any good info on how to fix.

Has anyone ran across this and know if it can be fixed? Telling users to just maximize their Outlook window worked for a little while, but not an ideal solution.











dotnet-csharpwindows-wpfoffice-addins-dev
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

gekka avatar image
1 Vote"
gekka answered ·
 namespace OutlookAddIn1
 {
     using System;
     using System.Threading.Tasks;
     using System.Windows;
     using System.Windows.Controls;
    
     using System.Windows.Input;
     using System.Windows.Media;
    
     public partial class VSTOComboBoxTestUserControl : System.Windows.Controls.UserControl
     {
         public VSTOComboBoxTestUserControl()
         {
             InitializeComponent();
    
             var combo = new ComboBox();
             combo.DropDownOpened += ComboBox_DropDownOpened;
    
             var s = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
             foreach (char c in s)
             {
                 combo.Items.Add(new string(c, 20));
             }
    
             this.Content = combo;
         }
    
         [System.Runtime.InteropServices.DllImport("user32.dll")]
         private static extern short GetAsyncKeyState(int virtualKeyCode);
         private const int VK_LBUTTON = 1;
    
         private async void ComboBox_DropDownOpened(object sender, EventArgs e)
         {
             var combo = (System.Windows.Controls.ComboBox)sender;
             if (Mouse.Captured == combo)
             {
                 while (combo.IsDropDownOpen && Mouse.Captured == combo)
                 {
                     if (Mouse.LeftButton == MouseButtonState.Released)
                     {
                         combo.LostMouseCapture += Combo_LostMouseCapture;
                         return;
                     }
                     await Task.Delay(1);
                 }
             }
         }
    
         private void Combo_LostMouseCapture(object sender, MouseEventArgs e)
         {
             var combo = (ComboBox)sender;
             combo.LostMouseCapture -= Combo_LostMouseCapture;
    
             if (Mouse.Captured != null)
             {
                 return; //Not Supported
             }
    
             var esckey = (GetAsyncKeyState(System.Windows.Input.KeyInterop.VirtualKeyFromKey(Key.Escape)) & 0x8000) == 0x8000;
             var lbutton = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) == 0x8000;
    
             if (esckey || !lbutton)
             {
                 return;
             }
    
             var d = e.MouseDevice.Target as DependencyObject;
             while (d != null)
             {
                 var comboItem = d as ComboBoxItem;
                 if (comboItem != null)
                 {
                     var index = combo.ItemContainerGenerator.IndexFromContainer(comboItem);
                     if (index >= 0)
                     {
                         if (comboItem.IsEnabled && comboItem.IsHitTestVisible && comboItem.IsMouseOver)
                         {
                             if (combo.SelectedIndex != index)
                             {
                                 combo.SelectedIndex = index;
    
                             }
                             return;
                         }
                     }
                 }
                 d = VisualTreeHelper.GetParent(d);
             }
    
         }
     }
 }
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yep so far that does seem to do the trick! Thanks for the help!

0 Votes 0 ·