wpf RibbonButton文本被截断

时间:2013-11-17 13:23:37

标签: wpf xaml ribbon

下一个XAML显示RibbonButton非常显示。 FontSize故意变得很大。

我能做些什么才能让事情变得更好?

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

2 个答案:

答案 0 :(得分:0)

在dkozl评论之后我意识到TextWrapping不是AttachedProperty, 我不知道r命名空间代表什么我猜它是来自现代的ui 任何方式它仍然是一个按钮,所以尝试使用它的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>

答案 1 :(得分:0)

我做了一个为我做这项工作的行为,也许这对你有用吗?

请注意我正在使用:System.Windows.Controls.Ribbon.dll(v4)

用法:

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

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