c# - WPF ComboBox not firing SelectionChanged event if placed at the bottom of Word AddIn -


i'm developing office word addin , using wpf controls user interface inside winforms usercontrol container (inside elementhost control).

the problem noticed combobox doesn't fire selectionchanged event if placed @ bottom of addin items near bottom of word application. instance, can click , choose first listed item (if lucky), otherwise combobox dropdown (popup) closed , selectionchanged event not fired. instead, word performs actions zooming or changing page layout - actions located @ right bottom of word application, if addin located @ right side.

the workaround found use combobox popup 'upsided'. in case, events firing well. said, workaround, , see smarter solution.

p.s.:if i'm using winforms combobox control, placing bottom doesn't cause issues - selectedindexchanged event works expected.

thanks

edit: added basic sample code.

sample code: wpfcontrol.xaml - ui

<usercontrol x:class="wordaddin.wpfcontrol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"               mc:ignorable="d"               d:designheight="300" d:designwidth="300">     <grid>         <grid.rowdefinitions>             <rowdefinition height="100*"></rowdefinition>             <rowdefinition height="40"></rowdefinition>         </grid.rowdefinitions>          <combobox x:name="cboitems" grid.row="1" margin="10,10,10,10"                   displaymemberpath="name"                   selectionchanged="cboitems_selectionchanged"/>     </grid> </usercontrol> 

sample code: wpfcontrol.xaml.cs - code behind

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes;  namespace wordaddin {     /// <summary>     /// interaction logic wpfcontrol.xaml     /// </summary>     public partial class wpfcontrol : usercontrol     {         public class item         {             public int id { get; set; }             public string name { get; set; }         }          public wpfcontrol()         {             initializecomponent();              list<item> itemlist = new list<item>();              (int = 1; < 11; i++)             {                 itemlist.add(new item { id = i, name = "item " + });             }              cboitems.itemssource = itemlist;         }          private void cboitems_selectionchanged(object sender, selectionchangedeventargs e)         {             if (e.addeditems != null && e.addeditems.count > 0)             {                 var item = ((object[])(e.addeditems)).tolist().firstordefault() item;                  messagebox.show("item: " + item.name + " clicked");                  e.handled = true;             }              e.handled = false;         }     } } 

wpfcontrainer contains element host , wpfcontrol element host child added addin on button click on word ribbon.

sample code: ribbonword.cs

private void btntest_click(object sender, ribboncontroleventargs e) {     wpfcontainer wpfcontainer = new wpfcontainer();      var wpfcontainerpane = globals.thisaddin.customtaskpanes.add(wpfcontainer, "addin");      wpfcontainerpane.visible = true; } 

i had same issue. selectionchanged not work when dropdown located outside of wpf. try change code in way

xaml:

   <combobox x:name="cboitems"               grid.row="1"               margin="10,10,10,10"              displaymemberpath="name"              selectionchanged="cboitems_selectionchanged"              dropdownopened="combobox_ondropdownopened"              dropdownclosed="combobox_ondropdownclosed" /> 

code behind:

namespace wordaddin {             public partial class wpfcontrol : usercontrol     {         // code          private dispatcherframe dispatcherframe;                     private object dispatcheroperationbegin(object arg)         {             dispatcherframe = new dispatcherframe();             dispatcher.pushframe(dispatcherframe);             return null;         }          private void combobox_ondropdownopened(object sender, eventargs e)         {             dispatcher.begininvoke(dispatcherpriority.normal, new dispatcheroperationcallback(dispatcheroperationbegin), null);         }          private void combobox_ondropdownclosed(object sender, eventargs e)         {             dispatcherframe.continue = false;         }     } } 

more information dispatcherframe can find here.


Comments