将省略号隐藏在AppBar中

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

右上角

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

输入图像描述

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

2
地图应用程序命令栏中似乎有省略号?单击它后,您可以选择打印、共享或反馈。我认为您与 SplitView 控件弄混了。此应用程序中的主菜单栏使用 SplitView。 - Daniel Jacobson
为了快速解决问题,我只是将Padding="0,0,-48,0"放置以隐藏它。 - Hendra Anggrian
5个回答

25

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

通用 Windows 应用程序的 CommandBar 控件已经得到改进,提供了超集的 AppBar 功能和更大的灵活性,可以在应用程序中更加自由地使用。在 Windows 10 上,您应该为所有新的通用 Windows 应用程序使用 CommandBar。

您可以在此处阅读更多信息。

无论是CommandBar还是AppBar都可以进行完全的样式和模板设置。这使您能够移除您不想显示的任何UI元素。

以下是如何操作 -

在 Blend 中打开页面,右键单击CommandBar > Edit Template > Edit a Copy。然后确保您选择Define in Application,因为当前的 Blend 存在一个错误,如果您选择This document,则会导致生成样式失败。

一旦您拥有了所有的样式,请找到 MoreButton 控件,并将其 Visibility 设置为 Collapsed(或者您可以删除它,但是如果您后来意识到需要它呢?)。

然后,您应该拥有一个没有省略号的CommandBar

2017年更新 现在可以在 CommandBarOverflowButtonVisibility 属性中找到省略号按钮的可见性。如上所述,将其设置为 Collapsed 可以隐藏它。


我已经成功隐藏了我的MoreButton。但是我也想默认显示图标标签/应用栏标签和按钮。哪个属性应该被更改? - Aakansha
@Aakansha,你可以尝试在页面加载时设置this.YourCommandBar.IsOpen = true,但是页面上的任何操作都会导致它再次折叠。 - Justin XL
我在进行这个操作时遇到了异常。异常信息是“未检测到已安装的组件。无法解析TargetName HighContrastBorder。” - Aakansha
我测试了它,它运行良好。也许你应该在一个问题中发布它? - Justin XL
1
对于那些无法在Blend中生成默认“CommandBar”样式的人来说,这里有解决方法。 - Justin XL
显示剩余2条评论

10
如果您想全局隐藏此按钮,只需添加:
<Style x:Key="EllipsisButton" TargetType="Button">
    <Setter Property="Visibility" Value="Collapsed"/>
</Style>

到全局资源文件


+1 这对修改 MoreButton 的样式也非常有用。 - testing

7

我知道这个问题已经不再活跃了,但为了完整起见,我提出我的答案。

与其使用样式更改可见性,我编写了一个附加属性扩展,可以通过数据绑定隐藏/显示MoreButton。这样,您可以根据需要有条件地显示/隐藏它。

使用起来非常简单,只需要将您的属性与扩展绑定即可:

<CommandBar extensions:CommandBarExtensions.HideMoreButton="{Binding MyBoolean}">
    ...
</CommandBar>

扩展代码如下:
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;
    }
}

在初始绑定时,它使用Loaded事件来应用隐藏,一旦加载完成就会隐藏。FindDescendantByName是另一个扩展方法,用于迭代可视树。如果您的解决方案中还没有此方法,您可能需要创建或获取一个。


我喜欢它,使用简单易懂。 - James Esh
此外,Michael Woolsay的回答包含了一个很好的子元素查找器。(https://dev59.com/IlwZ5IYBdhLWcg3wJ9cp#38055350) - James Esh

4

由于我无法在特定答案中添加评论,因此我将在此处发布。

以下页面提供了许多示例,可以找到子对象以补充@RadiusK的答案。

如何按名称或类型查找WPF控件?

在UWP中对我有效的一个是:

/// <summary>
/// Finds a Child of a given item in the visual tree. 
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
    // Confirm parent and childName are valid. 
    if (parent == null)
        return null;

    T foundChild = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);

        // If the child is not of the request child type child
        T childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild<T>(child, childName);

            // If the child is found, break so we do not overwrite the found child. 
            if (foundChild != null)
                break;
        }
        else if (!string.IsNullOrEmpty(childName))
        {
            var frameworkElement = child as FrameworkElement;

            // If the child's name is set for search
            if (frameworkElement != null && frameworkElement.Name == childName)
            {
                // if the child's name is of the request name
                foundChild = (T)child;
                break;
            }
        }
        else
        {
            // child element found.
            foundChild = (T)child;
            break;
        }
    }

    return foundChild;
}

这样调用代码:

var morebutton = FindChild<Button>(commandBar, "MoreButton");

0

在 @RadiusK 的回答基础上(其中存在一些问题),我想出了一个更简洁的替代方案,经过测试可以正常工作:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Linq
{
    public static class CommandBarExtensions
    {
        public static readonly DependencyProperty HideMoreButtonProperty = DependencyProperty.RegisterAttached("HideMoreButton", typeof(bool), typeof(CommandBarExtensions), new PropertyMetadata(false, OnHideMoreButtonChanged));
        public static bool GetHideMoreButton(CommandBar d)
        {
            return (bool)d.GetValue(HideMoreButtonProperty);
        }
        public static void SetHideMoreButton(CommandBar d, bool value)
        {
            d.SetValue(HideMoreButtonProperty, value);
        }
        static void OnHideMoreButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var CommandBar = d as CommandBar;
            if (CommandBar != null)
            {
                var MoreButton = CommandBar.GetChild<Button>("MoreButton") as UIElement;
                if (MoreButton != null)
                {
                    MoreButton.Visibility = !(e.NewValue as bool) ? Visibility.Visible : Visibility.Collapsed;
                }
                else CommandBar.Loaded += OnCommandBarLoaded;
            }
        }

        static void OnCommandBarLoaded(object sender, RoutedEventArgs e)
        {
            var CommandBar = sender as CommandBar;

            var MoreButton = CommandBar?.GetChild<Button>("MoreButton") as UIElement;
            if (MoreButton != null)
            {
                MoreButton.Visibility = !(GetHideMoreButton(CommandBar) as bool) ? Visibility.Visible : Visibility.Collapsed;
                CommandBar.Loaded -= OnCommandBarLoaded;
            }
        }

        public static T GetChild<T>(this DependencyObject Parent, string Name) where T : DependencyObject
        {
            if (Parent != null)
            {
                for (int i = 0, Count = VisualTreeHelper.GetChildrenCount(Parent); i < Count; i++)
                {
                    var Child = VisualTreeHelper.GetChild(Parent, i);

                    var Result = Child is T && !string.IsNullOrEmpty(Name) && (Child as FrameworkElement)?.Name == Name ? Child as T : Child.GetChild<T>(Name);
                    if (Result != null)
                        return Result;
                }
            }
            return null;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接