借鉴其他答案的思路,有几种方法可以不使用自定义派生类来完成。
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);
}
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](https://istack.dev59.com/7HZw0.webp)
省略折叠行代码会在标签原位置留下一个小凸起,这让我感到不舒服。
![http://i.imgur.com/eDQ8yVw.png](https://istack.dev59.com/eeX2L.webp)
如果您没有使用MVVM,也可以直接将该装入代码放入代码后面,或者使用EventToCommand。 这种方法不太可重用,但它具有相同的结果。
编辑:这是GetDescendant方法的代码:
public static class VisualTreeExtensions
{
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> { };
}
}
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> { };
}
}
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;
}
}
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;
}
}
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>();
}
}
}
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);
}
}
}
}