Centering a WPF Dialog Created from an Outlook VSTO Add-In

I am working on an Outlook 2010 add-in that provides a dialog box for user input. The code required to display the button on the ribbon is in the Outlook 2010 add-in's own project. This project has a link to the WPF user management library, which is responsible for the bulk of the work.

I use the static method in the WPF User Controls Library project, which is responsible for setting Caliburn.Micro correctly and displaying the dialog. All this works as expected, except that I cannot figure out how to properly position the dialog. I would like it to be displayed in the center of the Outlook window. I know that I have access to Microsoft.Office.Interop.Outlook.Application.ActiveWindow() , but I do not see how this helps me, since I can not translate it into PlacementTarget , as expected, in the settings of the Caliburn method .Micro WindowManager ShowDialog.

WPF Management 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(); 
+4
source share
1 answer

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

WPF Management 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); 
+4
source

Source: https://habr.com/ru/post/1397692/


All Articles