2015-11-07 61 views
0

我在我的WPF应用程序下面ribbonButton如何防止断裂线ribbonButton标签

<RibbonButton Style="{StaticResource MenuButton}"     
       Label="{x:Static res:Resources.SaveAs}" 
       LargeImageSource="..\Images\saveas.png" /> 

它看起来:saveAs with line break

,但我需要的东西,如:saveAs without line break

我使用非打破空间在xaml中:

<RibbonButton Style="{StaticResource MenuButton}"     
        Label="Zapisz&#160;jako" 
        LargeImageSource="..\Images\saveas.png" /> 

但是我由于本地化,还需要来自resx文件的字符串。但&#160;在resx中不起作用。有没有一些解决方案把非破坏空间放入resx?或者我应该修改ribbonButton模板?

回答

0

我做了一个行为,为我做的工作,也许它可以为你工作?

(看来你和我一样完全相同的问题...)

用法:

  <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(); 
     } 

     // ************************************************************************ 
    } 
}