1
votes

I am opening the WPF window with single combobox from an MS Excel add-in (VSTO) Ribbon button. 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. Once, selection is done the WPF window is shown infront of excel again. 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. 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. 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();
1
And BringToFront method doesn't work? How about trying the Win32 API SetForegroundWindowJeremy Thompson
Why do you create / show your window on a separate thread? Have you tried doing all that on the main thread?Dirk Vollmar
Yes i have tried it but eventually a separate thread is required in order not to have sideffects regarding interaction with Excel. As per my description the issue is not related to thread itself but setting up the owner of the window.Jim
@JeremyThompson Could you elaborate bit more? Bear in mind that I am not just setting an application to be on top but it should stay on top of excel window and show / hide accordingly, hence the setting of the Owner is required.Jim

1 Answers

0
votes

There is the TopMost property on the WPF Window definition: https://msdn.microsoft.com/en-us/library/system.windows.window.topmost.aspx

Set the TopMost property to True in your XAML definition:

<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.

Obviously if you built your WPFWindow using code you should set the Topmost property there.