将省略号隐藏在AppBar中

Jam*_* Ko 14 c# windows xaml winrt-xaml uwp

当您在UWP应用程序中创建AppBar或CommandBar时,控件侧面总会隐藏一个省略号,如下所示:

右上角

我不希望它在我的应用程序中但我没有找到任何方法/属性AppBar可以帮助我摆脱它.它应该是可能的,因为许多默认的Windows 10应用程序都没有它.例如,下面的主菜单栏上没有省略号:

在此输入图像描述

是否可以使用隐藏省略号AppBar,或者我是否必须使用SplitView或其他控件来实现它?

Jus*_* XL 21

首先,尽量不要AppBar在新的UWP应用中使用.

通用Windows应用程序的CommandBar控件已得到改进,可提供AppBar功能的超集,并在您的应用程序中使用它的方式具有更大的灵活性.您应该将CommandBar用于Windows 10上的所有新通用Windows应用程序.

你可以在这里阅读更多相关信息.

双方CommandBarAppBar可以充分的风格和模板.这使您能够删除不想显示的任何UI元素.

你就是这样做的 -

在Blend中打开页面,右键单击CommandBar >编辑模板>编辑副本.然后确保选择在应用程序中定义,因为当前在Blend中存在一个错误,如果您选择此文档,则无法生成样式.

一旦你拥有了所有样式,找到MoreButton控件并将其设置VisibilityCollapsed(或者你可以删除它,但如果你意识到以后需要它会怎样?).

那么你应该CommandBar没有省略号.

2017年更新 现在可以在a的OverflowButtonVisibility属性中找到省略号按钮的可见性CommandBar.如上所述设置Collapsed隐藏它.


Max*_*nov 9

如果要全局隐藏此按钮,则可以添加

<Style x:Key="EllipsisButton" TargetType="Button">
    <Setter Property="Visibility" Value="Collapsed"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

到全局资源文件


小智 7

我知道这个问题不再活跃,但为了完成,我提出了我的答案.

我没有通过使用样式来改变可见性,而是编写了一个AttachedProperty扩展,它可以通过数据绑定隐藏/显示MoreButton.这样您就可以随意显示/隐藏它.

用法就像将属性绑定到扩展名一样简单:

<CommandBar extensions:CommandBarExtensions.HideMoreButton="{Binding MyBoolean}">
    ...
</CommandBar>
Run Code Online (Sandbox Code Playgroud)

扩展代码如下:

public static class CommandBarExtensions
{
    public static readonly DependencyProperty HideMoreButtonProperty =
        DependencyProperty.RegisterAttached("HideMoreButton", typeof(bool), typeof(CommandBarExtensions), 
            new PropertyMetadata(false, OnHideMoreButtonChanged));

    public static bool GetHideMoreButton(UIElement element)
    {
        if (element == null) throw new ArgumentNullException(nameof(element));
        return (bool)element.GetValue(HideMoreButtonProperty);
    }

    public static void SetHideMoreButton(UIElement element, bool value)
    {
        if (element == null) throw new ArgumentNullException(nameof(element));
        element.SetValue(HideMoreButtonProperty, value);
    }

    private static void OnHideMoreButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var commandBar = d as CommandBar;
        if (e == null || commandBar == null || e.NewValue == null) return;
        var morebutton = commandBar.FindDescendantByName("MoreButton");
        if (morebutton != null)
        {
            var value = GetHideMoreButton(commandBar);
            morebutton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
        }
        else
        {
            commandBar.Loaded += CommandBarLoaded;
        }
    }

    private static void CommandBarLoaded(object o, object args)
    {
        var commandBar = o as CommandBar;
        var morebutton = commandBar?.FindDescendantByName("MoreButton");
        if (morebutton == null) return;
        var value = GetHideMoreButton(commandBar);
        morebutton.Visibility = value ?  Visibility.Collapsed : Visibility.Visible;
        commandBar.Loaded -= CommandBarLoaded;
    }
}
Run Code Online (Sandbox Code Playgroud)

在初始绑定时,它使用Loaded事件在加载后应用隐藏.这FindDescendantByName是迭代可视树的另一种扩展方法.如果您的解决方案尚未包含它,您可能想要创建或获取一个.