我做了一个对我有用的行为,也许它对你有用?
(看来您遇到的问题与我完全相同...)
用法:
<RibbonButton Command="macro:MacroCommands.CommandMacroSaveAs" Label="Save as"
SmallImageSource="pack://application:,,,/WpfUtil;component/Images/document-save-as16.png"
LargeImageSource="pack://application:,,,/WpfUtil;component/Images/document-save-as32.png"
RibbonTwoLineText.HasTwoLines="False"
>
<i:Interaction.Behaviors>
<behaviors:BehaviorRibbonButton TextWrapping="NoWrap" />
</i:Interaction.Behaviors>
</RibbonButton>
行为:
using System.Windows;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace HQ.Wpf.Util.Behaviors
{
public class BehaviorRibbonButton : Behavior<RibbonButton>
{
// ************************************************************************
public TextWrapping TextWrapping
{
get { return (TextWrapping)GetValue(TextWrappingProperty); }
set { SetValue(TextWrappingProperty, value); }
}
// ************************************************************************
public static readonly DependencyProperty TextWrappingProperty =
DependencyProperty.Register("TextWrapping", typeof(TextWrapping), typeof(BehaviorRibbonButton), new UIPropertyMetadata(TextWrapping.Wrap, TextWrappingPropertyChangedCallback));
// ************************************************************************
private static void TextWrappingPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var ribbonButton = dependencyObject as BehaviorRibbonButton;
if (ribbonButton != null)
{
ribbonButton.SetTextWrapping();
}
}
// ************************************************************************
public bool HasTwoLine
{
get
{
if (TextWrapping == TextWrapping.NoWrap)
{
return false;
}
return true;
}
}
// ************************************************************************
private void SetTextWrapping()
{
var ribbonButton = this.AssociatedObject as RibbonButton;
if (ribbonButton != null)
{
var ribbonTwoLineText = UiUtility.FindVisualChild<RibbonTwoLineText>(ribbonButton);
if (ribbonTwoLineText != null)
{
ribbonTwoLineText.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
var binding = new Binding("HasTwoLine");
binding.Source = this;
binding.Mode = BindingMode.OneWay;
ribbonTwoLineText.SetBinding(RibbonTwoLineText.HasTwoLinesProperty, binding);
}
}
}
// ************************************************************************
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += AssociatedObjectOnLoaded;
}
// ************************************************************************
private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
SetTextWrapping();
}
// ************************************************************************
}
}