0

我的 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 模板?

4

1 回答 1

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

        // ************************************************************************
    }
}
于 2016-09-08T14:44:14.927 回答