简体   繁体   English

VSTO Excel插件WPF UI窗口所有者

[英]VSTO Excel Addin WPF UI window owner

I am opening the WPF window with single combobox from an MS Excel add-in (VSTO) Ribbon button. 我正在使用MS Excel加载项(VSTO)功能区按钮中的单个组合框打开WPF窗口。 The issue is that in random windows environments upon click on combobox the dropdown selection options are shown in front of excel while the window itself get hidden behind it. 问题在于,在随机窗口环境中,单击组合框时,下拉选择选项显示在excel的前面,而窗口本身隐藏在它的后面。 Once, selection is done the WPF window is shown infront of excel again. 一旦选择完成,WPF窗口将再次显示在excel的前面。 If I remove the owner of window then both, combobox and window, are showing properly however I am loosing the effect of current window to stay permanently in front of current Excel window. 如果我删除了窗口的所有者,那么组合框和窗口都正常显示但是我正在失去当前窗口的效果以永久保留在当前Excel窗口的前面。 Again, I would like to point out that in most environments the bellow code is working fine except for windows 8 and occasionally in virtualbox hosted windows7. 同样,我想指出的是,在大多数环境中,波纹管代码都能正常工作,除了Windows 8以及偶尔在VirtualBox托管的Windows7中。 Any ideas what is wrong with my approach? 有什么想法我的方法有什么问题吗?

 var thread = new Thread(() =>
            {
                var wpfWindow = new WPFWindow();
                var ownerWindowHandle = (IntPtr)Globals.ThisAddIn.Application.Hwnd;


                var helper = new WindowInteropHelper(wpfWindow);
                helper.Owner = ownerWindowHandle; // COMMENT THAT AND IT WORKS PROPERLY
                wpfWindow.Show();
                wpfWindow.Closed += (sender2, e2) => wpfWindow.Dispatcher.InvokeShutdown();

                Dispatcher.Run();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

There is the TopMost property on the WPF Window definition: https://msdn.microsoft.com/en-us/library/system.windows.window.topmost.aspx WPF窗口定义中有TopMost属性: https//msdn.microsoft.com/en-us/library/system.windows.window.topmost.aspx

Set the TopMost property to True in your XAML definition: 在XAML定义中将TopMost属性设置为True:

<Window ...
         Topmost="True">
    <Grid>
        <TextBlock>test</TextBlock>
    </Grid>
</Window>

The ellipsis is where your other declarations goo is, the example just shows you how to add the Topmost property. 省略号是您其他声明的位置,该示例仅向您显示如何添加Topmost属性。

Obviously if you built your WPFWindow using code you should set the Topmost property there. 显然,如果您使用代码构建了WPFWindow,则应在此处设置Topmost属性。

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

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