将省略号隐藏在AppBar中

时间:2015-08-03 16:04:51

标签: c# windows xaml uwp winrt-xaml

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

top right corner

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

enter image description here

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

4 个答案:

答案 0 :(得分:21)

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

  

通用Windows应用程序的CommandBar控件已改进为   提供AppBar功能的超集和更大的灵活性   如何在您的应用中使用它。您应该将CommandBar用于所有新的   Windows 10上的通用Windows应用程序。

您可以详细了解here

CommandBarAppBar都可以是完整样式和模板化的。这使您能够删除不想显示的任何UI元素。

你就是这样做的 -

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

获得所有样式后,找到MoreButton控件并将其Visibility设置为Collapsed(或者您可以删除它,但如果您意识到以后需要它会怎样?)。

然后你应该有CommandBar没有省略号。

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

答案 1 :(得分:8)

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

<Style x:Key="EllipsisButton" TargetType="Button">
    <Setter Property="Visibility" Value="Collapsed"/>
</Style>

到全局资源文件

答案 2 :(得分:4)

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

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

How can I find WPF controls by name or type?

在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");

答案 3 :(得分: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;
        }
    }
}