右键菜单,tootip等弹出时,弹出的位置经常在左侧,使用体验不好。

弹出方向有左对齐和右对齐 SystemParameters.MenuDropAlignment

当右对齐时,值为false

可以在控制面板中查看当前的设置:

C# 系统菜单弹出框方向

 如何在软件中强制向右弹出显示?

可以设置SystemParameters.MenuDropAlignment值,因为没有公开属性的set方法,只能用反射去修改了。

 1     public static class MenuDropAlignmentHelper
 2     {
 3         private static FieldInfo _menuDropAlignmentField;
 4         public static void DisableSystemMenuAlignment()
 5         {
 6             _menuDropAlignmentField = typeof(SystemParameters).GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static);
 7 
 8             EnsureStandardPopupAlignment();
 9             SystemParameters.StaticPropertyChanged -= SystemParameters_StaticPropertyChanged;
10             SystemParameters.StaticPropertyChanged += SystemParameters_StaticPropertyChanged;
11         }
12 
13         private static void SystemParameters_StaticPropertyChanged(object sender, PropertyChangedEventArgs e)
14         {
15             EnsureStandardPopupAlignment();
16         }
17 
18         private static void EnsureStandardPopupAlignment()
19         {
20             if (SystemParameters.MenuDropAlignment)
21             {
22                 _menuDropAlignmentField?.SetValue(null, false);
23             }
24         }
25     }

之后在相应的窗口下,调用此方法MenuDropAlignmentHelper.DisableSystemMenuAlignment即可。

参考文章:

WPF Handedness with Popups

 

相关文章:

  • 2022-01-06
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2021-10-03
  • 2021-07-20
  • 2022-02-18
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-10-28
  • 2021-11-27
相关资源
相似解决方案