WPF ComboBox doesn't stay open when used in a Task Pane

时间秒杀一切 提交于 2019-12-07 18:31:01

问题


I have a strange bug with WPF Interop and an Excel Addin. I'm using .Net 3.5 SP1.

I'm using Add-in Express to create a Custom Task Pane for Excel 2003. Within that taskpane I'm using ElementHost to host a WPF UserControl. The UserControl simply contains a Grid with a TextBox and ComboBox. My problem is that whilst everything displays properly, the ComboBox won't stay dropped-down unless I hold the mouse down over the down-arrow.

I don't believe this is necessarily related to Add-in Express because I've had a similar problem when I tried displaying a WPF window modelessly in Excel.

A second problem is that the ComboBox seems reluctant to give up focus. If I click it, the text area goes grey to indicate that it has focus, but I can't move focus anywhere else in the window. The only way to wrest focus away is to move the mousewheel.

Anybody else had a similar problem, and managed to fix it?


回答1:


Add-in Express looked into this for me, and it turns out to have something to do with the Window style of the Task Pane that gets added to Excel. If you turn off the WS_CHILD flag in the Windows CreateParams then Combo Boxes and other popups work as expected.

They gave me this snippet of code to add to my ADXExcelTaskPane:

private const uint WS_CHILD = 0x40000000;
private const uint WS_CLIPCHILDREN = 0x02000000;
private const uint WS_CLIPSIBLINGS = 0x04000000;

private CreateParams _CreateParams = new CreateParams();
protected override CreateParams CreateParams
{
    get
    {
        _CreateParams = base.CreateParams;
        if (!DesignMode)
            _CreateParams.Style = (int)(WS_CLIPCHILDREN | WS_CLIPSIBLINGS); //| WS_CHILD

        return _CreateParams;
    }
}



回答2:


I had the same problem. I have a WPF user control hosted in a WinForm user control and the whole is an Excel AddIn. I work with Visual Studio 2010 and Excel 2007 and Excel 2010.

My problem was that when I clicked once in the Excel sheet, the AddIn never gains focus again. I found a workaround.

  1. In the constructor of my WinForm user control, I register on the event MouseEnter of my WPF user control.
  2. In the MouseEnter event handler, I give the focus to myself (this.Focus())

    public WpfContainerUserControl()
    {
        InitializeComponent();
        GpecsBrowserTabUserControl gpecBrowser = elementHost1.Child as GpecsBrowserTabUserControl;
        gpecBrowser.MouseEnter += new System.Windows.Input.MouseEventHandler(gpecBrowser_MouseEnter);
    }
    
    void gpecBrowser_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        this.Focus();
    } 
    


来源:https://stackoverflow.com/questions/183035/wpf-combobox-doesnt-stay-open-when-used-in-a-task-pane

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!