简体   繁体   English

如何防止RibbonButton标签中的折线

[英]How to prevent breaking line in ribbonButton label

I have following ribbonButton in my wpf application 我的wpf应用程序中有以下ribbonButton

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

It looks: saveAs with line break 看起来: 带有换行符的saveAs

but I need something like: saveAs without line break 但我需要这样的东西: saveAs不换行

I used non breaking space in xaml : 我在xaml中使用了不间断的空间:

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

But I also need that string from resx file because of localization. 但是由于本地化,我还需要从resx文件中获取该字符串。 But &#160; 但是&#160; doesn't work when it is in resx. 在resx中不起作用。 Is there some solution to put non breaking space into resx? 有一些解决方案可以将不间断的空间放入resx吗? Or should I modify ribbonButton template? 还是应该修改RibbonButton模板?

I did a behavior that does the job for me, perhaps it could works for you? 我的行为对我有帮助,也许对您有用?

(it seems that you get the exact same problem as I did...) (似乎您遇到了与我完全相同的问题...)

Usage: 用法:

            <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>

Behavior: 行为:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM