WPF功能区 - 隐藏选项卡标题(单选项卡应用程序)

时间:2014-05-05 04:29:32

标签: wpf ribbon

我是WPF新手,我正在尝试使用功能区控件。

我在应用程序中有一个标签,并希望隐藏标题但仍显示标签本身。

我一直在尝试各种属性和样式,但我只能隐藏整个标签。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

我设法通过将控件向上移动47个像素来隐藏标签页眉和应用程序菜单......

<r:Ribbon Margin="0,-47,0,0" DockPanel.Dock="Top" x:Name="ribbon">

注意:您可以通过执行此操作隐藏应用程序菜单而不隐藏选项卡...

<r:Ribbon DockPanel.Dock="Top" x:Name="ribbon">             
    <r:Ribbon.ApplicationMenu>
        <r:RibbonApplicationMenu Visibility="Collapsed" />
    </r:Ribbon.ApplicationMenu>

只隐藏标签标题,我不能这么做。通过覆盖功能区类,我确实非常接近如下......

class RibbonNoTab : Ribbon
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var ctrl = this.GetDescendants<Grid>().FirstOrDefault();
        if (ctrl != null)
        {
            ctrl.RowDefinitions[1].Height = new GridLength(0, System.Windows.GridUnitType.Pixel);
        }
    }
}

GetDescendants扩展方法只是在可视化树中搜索指定类型的内容。它来自这里:http://blog.falafel.com/finding-controls-by-type-in-silverlight-and-wpf/

上述方法的唯一问题是看起来像剩下的1像素高的条。你必须仔细观察才能看到它!

答案 1 :(得分:3)

我知道这已经超过一年了,但万一有其他人在这里钻研:

撇开其他答案,有几种方法可以在不使用自定义派生类的情况下完成

public class RibbonBehavior
{

    public static bool GetHideRibbonTabs(DependencyObject obj)
    {
        return (bool)obj.GetValue(HideRibbonTabsProperty);
    }

    public static void SetHideRibbonTabs(DependencyObject obj, bool value)
    {
        obj.SetValue(HideRibbonTabsProperty, value);
    }

    // Using a DependencyProperty as the backing store for HideRibbonTabs.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HideRibbonTabsProperty =
        DependencyProperty.RegisterAttached("HideRibbonTabs", typeof(bool), typeof(RibbonBehavior), new UIPropertyMetadata(false,OnHideRibbonTabsChanged));



    public static void OnHideRibbonTabsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d == null || d.GetType() != typeof(Ribbon)) return;

        (d as Ribbon).Loaded += ctrl_Loaded;

    }

    static void ctrl_Loaded(object sender, RoutedEventArgs e)
    {
        if (sender == null || sender.GetType() != typeof(Ribbon)) return;

        Ribbon _ribbon = (Ribbon)sender;

        var tabGrid = _ribbon.GetDescendants<Grid>().FirstOrDefault();
        tabGrid.RowDefinitions[1].Height = new GridLength(0, System.Windows.GridUnitType.Pixel);


        foreach (Line line in _ribbon.GetDescendants<Line>())
            line.Visibility = Visibility.Collapsed;

    }
}

然后在你的xaml:

<Ribbon SelectedIndex="0" a:RibbonBehavior.HideRibbonTabs="true">

这将为您提供一个没有标签的漂亮的方形带状框:

http://i.imgur.com/yCtt441.png

离开折叠线条的代码会留下一点点光圈,这就是以前的标签,这让我感到烦恼。

http://i.imgur.com/eDQ8yVw.png

如果您不使用MVVM,也可以将加载的代码直接放入代码中,如果不是,则可以在EventToCommand中。这种方式不太可重复使用,但结果相同。

编辑:这是GetDescendant方法的代码

 public static class VisualTreeExtensions
{
    /// <summary>
    /// Gets children, children’s children, etc. from 
    /// the visual tree that match the specified type
    /// </summary>
    public static List<T> GetDescendants<T>(this DependencyObject parent)
        where T : UIElement
    {
        List<T> children = new List<T>();
        int count = VisualTreeHelper.GetChildrenCount(parent);
        if (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
                if (child is T)
                {
                    children.Add((T)child);
                }
                children.AddRange(child.GetDescendants<T>());
            }
            return children;
        }
        else
        {
            return new List<T> { };
        }
    }
    /// <summary>
    /// Gets children, children’s children, etc. from 
    /// the visual tree that match the specified type and elementName
    /// </summary>
    public static List<T> GetDescendants<T>(this DependencyObject parent, string elementName)
        where T : UIElement
    {
        List<T> children = new List<T>();
        int count = VisualTreeHelper.GetChildrenCount(parent);
        if (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
                if (child is T && (child is FrameworkElement)
                    && (child as FrameworkElement).Name == elementName)
                {
                    children.Add((T)child);
                }
                children.AddRange(child.GetDescendants<T>(elementName));
            }
            return children;
        }
        else
        {
            return new List<T> { };
        }
    }
    /// <summary>
    /// Gets the first child, child’s child, etc. 
    /// from the visual tree that matches the specified type
    /// </summary>
    public static T GetDescendant<T>(this DependencyObject parent)
        where T : UIElement
    {
        List<T> descendants = parent.GetDescendants<T>();
        if (descendants.Count > 0)
        {
            return descendants[0];
        }
        else
        {
            return null;
        }
    }
    /// <summary>
    /// Gets the first child, child’s child, etc. from 
    /// the visual tree that matches the specified type and elementName
    /// </summary>
    public static T GetDescendant<T>(this DependencyObject parent, string elementName)
        where T : UIElement
    {
        List<T> descendants = parent.GetDescendants<T>(elementName);
        if (descendants.Count > 0)
        {
            return descendants[0];
        }
        else
        {
            return null;
        }
    }
    /// <summary>
    /// Gets the first parent, parent’s parent, etc. from the 
    /// visual tree that matches the specified type
    /// </summary>
    public static T GetAntecedent<T>(this DependencyObject root)
        where T : UIElement
    {
        if (root == null)
        {
            return null;
        }
        if (root is T)
        {
            return (T)root;
        }
        else
        {
            DependencyObject parent = VisualTreeHelper.GetParent(root);
            if (parent == null)
            {
                return null;
            }
            else
            {
                return parent.GetAntecedent<T>();
            }
        }
    }
    /// <summary>
    /// Gets the first parent, parent’s parent, etc. from the 
    /// visual tree that matches the specified type and elementName
    /// </summary>
    public static T GetAntecedent<T>(this DependencyObject root, string elementName)
        where T : UIElement
    {
        if (root == null)
        {
            return null;
        }
        if (root is T && (root is FrameworkElement)
            && (root as FrameworkElement).Name == elementName)
        {
            return (T)root;
        }
        else
        {
            DependencyObject parent = VisualTreeHelper.GetParent(root);
            if (parent == null)
            {
                return null;
            }
            else
            {
                return parent.GetAntecedent<T>(elementName);
            }
        }
    }
}

答案 2 :(得分:0)

关于flobadob的回答: 将控件向上移动47像素是一个非常有趣的解决方案。

如果要保留通常位于功能区菜单上方的行,请移动控件,然后在代码中添加一行。以下内容适用:

std::vector<std::string> strvec;
strvec.emplace_back("abc")  //calls string(const char*)

strvec.push_back("abc")          //is equivalent to...
strvec.push_back(string("abc"))  //which calls string(const char*) then string(string&&)

用背景颜色替换白色填充。另请注意,功能区是网格行1,而后面放置的行是网格行0。