简体   繁体   English

将焦点保持在外接程序的WPF组合框中

[英]keep focus in a WPF combo box in an addin

I have a combobox in a Word addin. 我在Word加载项中有一个组合框。 The contents of the combobox are often long enough to drape over Word's Zoom slider-bar control. 组合框的内容通常足够长,可以覆盖在Word的“缩放”滑块控件上。 However, selecting an item directly over the zoom control (which is hidden from view) causes the zoom control to get focus, close the combobox, and change your zoom setting! 但是,直接在缩放控件(从视图中隐藏)上选择一个项目会导致缩放控件获得焦点,关闭组合框并更改缩放设置! The selection in the combobox is untouched. 组合框中的选择保持不变。

How can I make the combo box keep focus and change the selected value to the item (over the zoom bar) selected? 如何使组合框保持焦点并将所选值更改为所选项目(在缩放栏上)? Thanks... 谢谢...

I've run into this same issue with WPF, and it looks like it has something to do with the way Word is handling events from child windows. 我在WPF中也遇到过同样的问题,它似乎与Word处理子窗口事件的方式有关。 Whenever the dropdown list (or probably any other "popup" control like a context menu) is drawn over one of Word's windows, it gets a bit greedy and assumes you're clicking on the underlying window. 每当在Word的一个窗口上绘制下拉列表(或可能的任何其他“弹出”控件,如上下文菜单)时,它都会有点贪婪,并假定您正在单击基础窗口。

I don't know a whole lot about how messaging/events works in Windows and I haven't had time to figure out the best way to address the problem, but based on a post somewhere about creating borderless windows I tried modifying the window styles of the WinForms user control as follows (windows style constants from http://www.pinvoke.net/default.aspx/Constants/Window%20styles.html ): 我对Windows中的消息传递/事件的工作方式不甚了解,我还没有时间找出解决此问题的最佳方法,但是基于关于创建无边界窗口的文章,我尝试修改窗口样式WinForms用户控件的外观如下(来自http://www.pinvoke.net/default.aspx/Constants/Window%20styles.html的 Windows样式常量):

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams p = base.CreateParams;
            if (!DesignMode)
            {
                unchecked
                {
                    p.Style = (int)(WindowStyles.WS_VISIBLE |
                                    WindowStyles.WS_POPUP |
                                    WindowStyles.WS_CLIPSIBLINGS |
                                    WindowStyles.WS_CLIPCHILDREN);
                }
            }
            return p;
        }
    } 

Curiously (or maybe not so curiously for people that are more familiar with Windows messages), the dropdown DOES respond to keyboard events (eg click to pop up the list, then use the keyboard to select an item). 奇怪的是(对于熟悉Windows消息的人可能不是那么好奇),下拉菜单会响应键盘事件(例如,单击以弹出列表,然后使用键盘选择一个项目)。

Functionally there doesn't seem to be a problem with the above code...but I'm not sure what the ramifications are of saying the user control is a popup instead of a child. 从功能上讲,上面的代码似乎没有问题...但是我不确定后果是什么,说用户控件是弹出窗口而不是子控件。

Another post that relates to this is WPF ComboBox doesn't stay open when used in a Task Pane . 与此相关的另一篇文章是WPF ComboBox在任务窗格中使用时不会保持打开状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM