3

My WPF application is Per-Monitor Aware and normally scales well. But when the app window is on a second monitor with scale=150% and I open a context menu it uses scale=100% from the main display making menu items quite small:

(Interestingly the submenu items use correct 150% scale)

I open the context menu setting IsOpen = true:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ContextMenu menu = new ContextMenu();

            menu.Items.Add("item 1");
            menu.Items.Add("item 2");

            MenuItem submenu = new MenuItem();
            submenu.Header = "submenu";
            submenu.Items.Add("more 1");
            submenu.Items.Add("more 2");
            menu.Items.Add(submenu);

            menu.IsOpen = true;
        }

How to configure context menu to use display settings it is opened on, not from the default main display?

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66

1 Answers1

0

You can RenderTransform to undo Scaling from PrimaryScreen and Scale to dpiScale from Visual:

var visual = e.Source as Visual;
if (visual != null)
{
     var dpiScale = VisualTreeHelper.GetDpi(visual);
     System.Windows.Forms.Screen.PrimaryScreen.
           GetDpi(DpiType.Effective, out uint dpiX, out uint dpiY);
     menu.RenderTransform = 
          new ScaleTransform(dpiScale.DpiScaleX / ((double)dpiX / 96), 
                            dpiScale.DpiScaleY / ((double)dpiY / 96));
     menu.IsOpen = true;
}
gReX
  • 1,060
  • 1
  • 20
  • 35