我是WPF的新手,正在尝试使用功能区控件。
我在应用程序中只有一个选项卡,希望隐藏标题但仍显示选项卡本身。
我一直在尝试各种属性和样式,但是我只能隐藏整个选项卡。
我尝试过:ribbontab可见性,ribbontab.header可见性,隐藏在TabHeaderItemCollection中的设置,将样式xaml应用于ribbontab中的ribbontabheader元素,尝试使用tabheadertemplate属性,并且通常在api中进行筛选以寻找可能相关的任何内容。
Google只显示了如何隐藏整个标签。
还有其他想法吗?
我设法通过将控件向上移动 47 个像素来隐藏选项卡标题和应用程序菜单......
<r:Ribbon Margin="0,-47,0,0" DockPanel.Dock="Top" x:Name="ribbon">
Run Code Online (Sandbox Code Playgroud)
注意:您可以通过执行此操作仅隐藏应用程序菜单而不隐藏选项卡...
<r:Ribbon DockPanel.Dock="Top" x:Name="ribbon">
<r:Ribbon.ApplicationMenu>
<r:RibbonApplicationMenu Visibility="Collapsed" />
</r:Ribbon.ApplicationMenu>
Run Code Online (Sandbox Code Playgroud)
仅隐藏选项卡标题,我不能完全做到。通过如下覆盖功能区类,我确实非常接近......
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
GetDescendants 扩展方法只是在可视化树中搜索指定类型的内容。取自这里:http : //blog.falafel.com/finding-controls-by-type-in-silverlight-and-wpf/
上述方法的唯一问题是看起来像剩余 1 像素高的条。你必须仔细观察才能看到它!
借助其他答案,有几种方法可以在不使用自定义派生类的情况下做到这一点
\n\npublic class RibbonBehavior\n{\n\n public static bool GetHideRibbonTabs(DependencyObject obj)\n {\n return (bool)obj.GetValue(HideRibbonTabsProperty);\n }\n\n public static void SetHideRibbonTabs(DependencyObject obj, bool value)\n {\n obj.SetValue(HideRibbonTabsProperty, value);\n }\n\n // Using a DependencyProperty as the backing store for HideRibbonTabs. This enables animation, styling, binding, etc...\n public static readonly DependencyProperty HideRibbonTabsProperty =\n DependencyProperty.RegisterAttached("HideRibbonTabs", typeof(bool), typeof(RibbonBehavior), new UIPropertyMetadata(false,OnHideRibbonTabsChanged));\n\n\n\n public static void OnHideRibbonTabsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n {\n if (d == null || d.GetType() != typeof(Ribbon)) return;\n\n (d as Ribbon).Loaded += ctrl_Loaded;\n\n }\n\n static void ctrl_Loaded(object sender, RoutedEventArgs e)\n {\n if (sender == null || sender.GetType() != typeof(Ribbon)) return;\n\n Ribbon _ribbon = (Ribbon)sender;\n\n var tabGrid = _ribbon.GetDescendants<Grid>().FirstOrDefault();\n tabGrid.RowDefinitions[1].Height = new GridLength(0, System.Windows.GridUnitType.Pixel);\n\n\n foreach (Line line in _ribbon.GetDescendants<Line>())\n line.Visibility = Visibility.Collapsed;\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n然后在你的xaml中:
\n\n<Ribbon SelectedIndex="0" a:RibbonBehavior.HideRibbonTabs="true">\nRun Code Online (Sandbox Code Playgroud)\n\n这将为您提供一个漂亮的方形丝带盒,没有标签:
\n\n\n\n省略折叠行的代码会在选项卡原来所在的位置留下一点点,这让我很困扰。
\n\n\n\n如果您不使用 MVVM,您也可以将加载的代码直接放入后面的代码中,如果使用的话,也可以将其放入 EventToCommand 中。这种方式可重用性较差,但结果相同。
\n\n编辑:这是 GetDescendant 方法的代码
\n\n public static class VisualTreeExtensions\n{\n /// <summary>\n /// Gets children, children\xe2\x80\x99s children, etc. from \n /// the visual tree that match the specified type\n /// </summary>\n public static List<T> GetDescendants<T>(this DependencyObject parent)\n where T : UIElement\n {\n List<T> children = new List<T>();\n int count = VisualTreeHelper.GetChildrenCount(parent);\n if (count > 0)\n {\n for (int i = 0; i < count; i++)\n {\n UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);\n if (child is T)\n {\n children.Add((T)child);\n }\n children.AddRange(child.GetDescendants<T>());\n }\n return children;\n }\n else\n {\n return new List<T> { };\n }\n }\n /// <summary>\n /// Gets children, children\xe2\x80\x99s children, etc. from \n /// the visual tree that match the specified type and elementName\n /// </summary>\n public static List<T> GetDescendants<T>(this DependencyObject parent, string elementName)\n where T : UIElement\n {\n List<T> children = new List<T>();\n int count = VisualTreeHelper.GetChildrenCount(parent);\n if (count > 0)\n {\n for (int i = 0; i < count; i++)\n {\n UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);\n if (child is T && (child is FrameworkElement)\n && (child as FrameworkElement).Name == elementName)\n {\n children.Add((T)child);\n }\n children.AddRange(child.GetDescendants<T>(elementName));\n }\n return children;\n }\n else\n {\n return new List<T> { };\n }\n }\n /// <summary>\n /// Gets the first child, child\xe2\x80\x99s child, etc. \n /// from the visual tree that matches the specified type\n /// </summary>\n public static T GetDescendant<T>(this DependencyObject parent)\n where T : UIElement\n {\n List<T> descendants = parent.GetDescendants<T>();\n if (descendants.Count > 0)\n {\n return descendants[0];\n }\n else\n {\n return null;\n }\n }\n /// <summary>\n /// Gets the first child, child\xe2\x80\x99s child, etc. from \n /// the visual tree that matches the specified type and elementName\n /// </summary>\n public static T GetDescendant<T>(this DependencyObject parent, string elementName)\n where T : UIElement\n {\n List<T> descendants = parent.GetDescendants<T>(elementName);\n if (descendants.Count > 0)\n {\n return descendants[0];\n }\n else\n {\n return null;\n }\n }\n /// <summary>\n /// Gets the first parent, parent\xe2\x80\x99s parent, etc. from the \n /// visual tree that matches the specified type\n /// </summary>\n public static T GetAntecedent<T>(this DependencyObject root)\n where T : UIElement\n {\n if (root == null)\n {\n return null;\n }\n if (root is T)\n {\n return (T)root;\n }\n else\n {\n DependencyObject parent = VisualTreeHelper.GetParent(root);\n if (parent == null)\n {\n return null;\n }\n else\n {\n return parent.GetAntecedent<T>();\n }\n }\n }\n /// <summary>\n /// Gets the first parent, parent\xe2\x80\x99s parent, etc. from the \n /// visual tree that matches the specified type and elementName\n /// </summary>\n public static T GetAntecedent<T>(this DependencyObject root, string elementName)\n where T : UIElement\n {\n if (root == null)\n {\n return null;\n }\n if (root is T && (root is FrameworkElement)\n && (root as FrameworkElement).Name == elementName)\n {\n return (T)root;\n }\n else\n {\n DependencyObject parent = VisualTreeHelper.GetParent(root);\n if (parent == null)\n {\n return null;\n }\n else\n {\n return parent.GetAntecedent<T>(elementName);\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3119 次 |
| 最近记录: |