WPF ToolBar: how to remove grip and overflow

后端 未结 6 2396
有刺的猬
有刺的猬 2020-12-07 14:29

In a nested WPF ToolBarPanel-ToolBar-Menu we want to get rid of the grip handle to the left and the overflow area to the right. they are both grayed out, but we\'d like them

6条回答
  •  春和景丽
    2020-12-07 15:09

    Rather than hiding the overflow button completely, I think it's better to show it only when necessary. This can be done by binding its Visibility property to its IsEnabled property:

    private static void FixupToolBarOverflowArrow(ToolBar toolBar)
    {
        Action fixup = () =>
        {
            var overflowButton = toolBar.Template.FindName("OverflowButton", toolBar) as ButtonBase;
            if (overflowButton != null)
            {
                overflowButton.SetBinding(
                    VisibilityProperty,
                    new Binding("IsEnabled")
                    {
                        RelativeSource = RelativeSource.Self,
                        Converter = new BooleanToVisibilityConverter()
                    });
            }
        };
    
        if (toolBar.IsLoaded)
        {
            fixup();
        }
        else
        {
            RoutedEventHandler handler = null;
            handler = (sender, e) =>
            {
                fixup();
                toolBar.Loaded -= handler;
            };
    
            toolBar.Loaded += handler;
        }
    }
    

    (the same thing can be done in XAML by redefining the template)

提交回复
热议问题