【问题标题】:Hiding the ellipsis within an AppBar在 AppBar 中隐藏省略号
【发布时间】:2015-10-25 19:57:17
【问题描述】:

当你在 UWP 应用中创建 AppBar 或 CommandBar 时,总是有一个省略号隐藏在控件的侧面附近,如下所示:

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

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

【问题讨论】:

  • 地图应用命令栏中好像有省略号?当您单击它时,您可以选择打印、共享或反馈。我认为您对 SplitView 控件有误。此应用程序的主菜单栏使用 SplitView。
  • 为了快速破解,我只是把 Padding="0,0,-48,0" 隐藏起来。

标签: c# windows xaml uwp winrt-xaml


【解决方案1】:

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

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

您可以阅读更多关于它的信息here

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

这就是你的做法-

在 Blend 中打开您的页面,右键单击 CommandBar> 编辑模板 > 编辑副本。然后确保您选择 Define in Application,因为目前 Blend 中存在一个错误,如果您选择 This document,将无法生成样式。

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

那么你应该有一个没有省略号的CommandBar

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

【讨论】:

  • 我可以隐藏我的 MoreButton。但我也想默认显示图标标签/应用栏标签以及按钮。应该为此更改哪个属性?
  • @Aakansha,您可以尝试在页面加载时设置this.YourCommandBar.IsOpen = true,但页面上的任何操作都会导致它再次折叠。
  • 执行此操作时出现异常。异常消息是“未检测到已安装的组件。无法解析 TargetName HighContrastBorder。”
  • 我测试了它,它运行良好。也许您应该在问题中发布它?
  • 对于那些未能在 Blend 中生成默认 CommandBar 样式的人。 Here 是。
【解决方案2】:

如果你想全局隐藏这个按钮,添加就足够了

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

到全局资源文件

【讨论】:

  • +1 这对于修改MoreButton的样式也很有用
【解决方案3】:

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

我没有使用样式更改可见性,而是编写了一个 AttachedProperty 扩展,它能够通过数据绑定隐藏/显示更多按钮。这样您就可以根据需要有条件地显示/隐藏它。

使用就像将你的属性绑定到扩展一样简单:

<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 是另一种迭代可视化树的扩展方法。如果您的解决方案尚未包含它,您可能想要创建或获取它。

【讨论】:

【解决方案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");

【讨论】:

    【解决方案5】:

    基于@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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 2016-01-31
      • 2018-02-28
      • 2018-03-14
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多