简体   繁体   English

WPF 功能区按钮文本被切断

[英]WPF ribbon button text is cut off

Next XAML shows a RibbonButton awfully displayed.下一个 XAML 显示了一个非常显示的RibbonButton The FontSize is intentionally made big. FontSize是故意变大的。

What can I do to get things better?我该怎么做才能让事情变得更好?

<UserControl
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <r:Ribbon FontSize="20">
            <r:RibbonTab Header="Perfectly nice header" >
                <r:RibbonButton Label="Text cut off" />
            </r:RibbonTab>
        </r:Ribbon>
    </Grid>
</UserControl>

Well after dkozl comment I realized that TextWrapping is not an AttachedProperty .dkozl评论之后,我意识到TextWrapping不是AttachedProperty I don't know what r namespace stands for.我不知道r命名空间代表什么。 I'm guessing it's from modern-UI any ways it's still a button so try playing with it's ContentTemplate and doing something similar to this:我猜它来自现代用户界面,无论如何它仍然是一个按钮,所以尝试使用它的ContentTemplate并执行类似的操作:

 <r:Ribbon FontSize="20">
     <r:RibbonTab Header="Perfectly nice header" >
         <r:RibbonButton Label="Text cut off">
            <r:RibbonButton.ContentTemplate>
                 <TextBlock Text="{Binding Label}" TextWrapping="Wrap" />
                 <!-- Label or Content or what ever holds your text.-->
            </r:RibbonButton.ContentTemplate>   
         </r:RibbonButton>
     </r:RibbonTab>
  </r:Ribbon>

I did a behavior that does the job for me, perhaps it could works for you?我做了一个对我有用的行为,也许它对你有用?

Please note that I'm using: System.Windows.Controls.Ribbon.dll (v4)请注意,我正在使用:System.Windows.Controls.Ribbon.dll (v4)

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