Megan
Megan

Reputation: 2594

Centering WPF dialog spawned from VSTO Outlook Add-In

I'm working on an Outlook 2010 add-in that provides a dialog for user input. The code necessary for displaying the button in the ribbon is in its own Outlook 2010 Add-in project. That project has a reference to a WPF User Control Library that is responsible for the bulk of the work.

I use a static method in the WPF User Control Library project that is responsible for configuring Caliburn.Micro correctly and displaying the dialog. All of this works as expected except that I cannot figure out how to correctly position the dialog. I would like it to display centered over the Outlook window. I know I have access to the Microsoft.Office.Interop.Outlook.Application.ActiveWindow(), but I do not see how that helps me since I cannot translate it to a PlacementTarget as expected in the settings for Caliburn.Micro WindowManager's ShowDialog method.

WPF User Control Library

namespace WpfUserControlLibrary {
    public static class Connector {
        public static void ShowDialog() {
            new AppBootstrapper();
            var windowManager = IoC.Get<IWindowManager>();
            windowManager.ShowDialog( new ShellViewModel() );
        }
    }
}

Outlook 2010 Add-in

WpfUserControlLibrary.Connector.ShowDialog();

Upvotes: 4

Views: 1800

Answers (1)

Megan
Megan

Reputation: 2594

I was able to track down a solution. Thanks to the help of this question, I was able to pass the appropriate parent window location and size parameters to the Connector. I checked the Caliburn.Micro source and noticed that I'm actually creating a ChildWindow--not a Popup. Therefore, I just needed to set the Top and Left values of the settings for the dialog.

WPF User Control Library

namespace WpfUserControlLibrary {
    public static class Connector {
        public static void ShowDialog(System.Windows.Rect parent) {
            new AppBootstrapper();
            var windowManager = IoC.Get<IWindowManager>();

            // Popup is always 600 x 400
            dynamic settings = new System.Dynamic.ExpandoObject();
            settings.Left = (parent.Left + parent.Width / 2) - 300;
            settings.Top = (parent.Top + parent.Height / 2) - 200;

            windowManager.ShowDialog(new ShellViewModel(), settings: settings);
        }
    }
}

Outlook 2010 Add-in

var win = ThisAddIn.Application.ActiveWindow();
var parent = new System.Windows.Rect(win.Left, win.Top, win.Width, win.Height);
WpfUserControlLibrary.Connector.ShowDialog(parent);

Upvotes: 4

Related Questions