1
votes

I created Outlook VSTO application. I want to popup a WPF window dialog when click button. Here is my WPF window:

<Window x:Class="WorkflowSR.View.ArchiveSettingWindow"
             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" 
             xmlns:local="clr-namespace:WorkflowSR.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <CheckBox x:Name="checkBox" Content="CheckBox" HorizontalAlignment="Left" Margin="111,73,0,0" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Window>

In code, when I want to open dialog, I do like this:

var archiveSettingWindow = new ArchiveSettingWindow();
archiveSettingWindow.owner = ???
archiveSettingWindow.ShowDialog();

What should I set for the window owner? Thank you.

1

1 Answers

3
votes

Use IOleWindow and WindowInteropHelper:

        using System.Runtime.InteropServices;
        ...
        IntPtr wnd = new IntPtr(0);         
        object window = _application.ActiveWindow();
        if (window != null)
        {
            IOleWindow oleWindow = window as IOleWindow;
            if (oleWindow != null)
            {
                oleWindow.GetWindow(out wnd);
            }
        }
        ...

        if (wnd != IntPtr.Zero)
        {
            WindowInteropHelper helper = new WindowInteropHelper(archiveSettingWindow);
            helper.Owner = wnd;
            archiveSettingWindow.ShowInTaskbar = false;
        }