如何防止ribbonButton标签中的断行

时间:2015-11-07 10:05:32

标签: c# wpf xaml resx ribbon-button

我的wpf应用程序中有以下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模板吗?

1 个答案:

答案 0 :(得分: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();
        }

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