WPF - How to style the menu control to remove the left margin?

后端 未结 9 1542
余生分开走
余生分开走 2021-02-04 08:41

I have added a default menu control to my user control. I need to style the menu to remove the left margin containing the space for the icon or checkbox. How can I do this?

9条回答
  •  猫巷女王i
    2021-02-04 09:08

    Thanks for succesfull idea. For .net Framework 4.5 and VS 2012 I wrote for ContextMenu and MenuItem accordingly:

            private const double ICON_SIZE = 32;
    
        void ContextMenu_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_pointerControl.ContextMenu.Template != null)
            {
                System.Windows.Shapes.Rectangle r1 = _pointerControl.ContextMenu.Template.FindName("3_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle;
                System.Windows.Shapes.Rectangle r2 = _pointerControl.ContextMenu.Template.FindName("4_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle;
                System.Windows.Shapes.Rectangle r3 = _pointerControl.ContextMenu.Template.FindName("5_T", _pointerControl.ContextMenu) as System.Windows.Shapes.Rectangle;
                double width = Math.Max(28, ICON_SIZE+14);
                r1.Width = width;
                r2.Margin = new System.Windows.Thickness(width + 1, 2, 0, 2);
                r3.Margin = new System.Windows.Thickness(width + 2, 2, 0, 2);
    
            }
        }
    
        void mi_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            System.Windows.Controls.MenuItem mi = sender as System.Windows.Controls.MenuItem;
            if (mi != null && mi.Template != null)
            {
                System.Windows.Controls.ContentPresenter cp = mi.Template.FindName("Icon", mi) as System.Windows.Controls.ContentPresenter;
                cp.Height = ICON_SIZE + 6;
                cp.Width = ICON_SIZE + 6;
            }
    
        }
    

提交回复
热议问题