WPF功能区按钮文本未显示在行中

时间:2016-01-24 13:32:54

标签: c# wpf xaml

以下是我的代码

<RibbonWindow x:Class="WPFRibbonButonNameTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Ribbon x:Name="RibbonWin" SelectedIndex="0" Background="Transparent">
            <RibbonTab Header="Tab 1" Selector.IsSelected="True">
                <RibbonGroup x:Name="grpNewPurchasing" Header="">
                    <RibbonButton x:Name="btnNewHirePurchase" Label="My Test Buton" Width="200" LargeImageSource="Images/cart1.png" KeyTip="N"/>
                </RibbonGroup>
            </RibbonTab>
        </Ribbon>
    </Grid>
</DockPanel>

上面的功能区按钮文本显示为两行。我想在一行中显示文本。我该如何正确地做到这一点?

请参阅附图。

Screen shot

2 个答案:

答案 0 :(得分:2)

正如@Filip所说,你必须为此修改ControlTemplate。这是您必须在ControlTemplate的{​​{1}}集合中修改的触发器之一。

将其添加到Triggers的{​​{1}}集合中,或者更好地添加到单独的RibbonWindow中。

Resources

有一个ResourceDictionary设置RibbonTwoLineText.HasTwoLines附加属性,上面我们删除了它。就是这部分:

<Style TargetType="{x:Type RibbonButton}" BasedOn="{StaticResource {x:Type RibbonButton}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RibbonButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                <Border BorderThickness="{TemplateBinding Border.BorderThickness}" CornerRadius="{TemplateBinding RibbonControlService.CornerRadius}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="OuterBorder" SnapsToDevicePixels="True">
                    <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" CornerRadius="{TemplateBinding RibbonControlService.CornerRadius}" BorderBrush="#00FFFFFF" Name="InnerBorder">
                        <StackPanel Name="StackPanel">
                            <Image Source="{TemplateBinding RibbonControlService.LargeImageSource}" Name="PART_Image" Width="32" Height="32" Margin="{DynamicResource {ComponentResourceKey TypeInTargetAssembly=Ribbon, ResourceId=LargeImageMargin}}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="Center" RenderOptions.BitmapScalingMode="NearestNeighbor" />
                            <Grid Name="Grid" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <RibbonTwoLineText TextAlignment="Center" LineHeight="13" LineStackingStrategy="BlockLineHeight" Text="{TemplateBinding RibbonControlService.Label}" Name="TwoLineText" Margin="1,1,1,0" HorizontalAlignment="Center" VerticalAlignment="Top" />
                            </Grid>
                        </StackPanel>
                    </Border>
                </Border>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Mode=Self}}" Value="Large">
                        <Setter Property="FrameworkElement.MinWidth" Value="44" />
                        <Setter Property="FrameworkElement.Height" Value="66" />
                        <Setter Property="FrameworkElement.MinHeight" TargetName="Grid" Value="26" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Mode=Self}}" Value="Small">
                        <Setter Property="FrameworkElement.Height" Value="22" />
                        <Setter Property="FrameworkElement.Margin" TargetName="PART_Image" Value="1,0,1,0" />
                        <Setter Property="Image.Source" TargetName="PART_Image" Value="{Binding SmallImageSource, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="FrameworkElement.Width" TargetName="PART_Image" Value="16" />
                        <Setter Property="FrameworkElement.Height" TargetName="PART_Image" Value="16" />
                        <Setter Property="FrameworkElement.HorizontalAlignment" TargetName="TwoLineText" Value="Left" />
                        <Setter Property="FrameworkElement.Margin" TargetName="TwoLineText" Value="1,1,1,1" />
                        <Setter Property="StackPanel.Orientation" TargetName="StackPanel" Value="Horizontal" />
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Mode=Self}}" Value="Small" />
                            <Condition Binding="{Binding Path=IsInQuickAccessToolBar, RelativeSource={RelativeSource Mode=Self}}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="FrameworkElement.Height" Value="NaN" />
                    </MultiDataTrigger>
                    <DataTrigger Binding="{Binding Path=ControlSizeDefinition.IsLabelVisible, RelativeSource={RelativeSource Mode=Self}}" Value="False">
                        <Setter Property="UIElement.Visibility" TargetName="TwoLineText" Value="Collapsed" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=ControlSizeDefinition.ImageSize, RelativeSource={RelativeSource Mode=Self}}" Value="Collapsed">
                        <Setter Property="UIElement.Visibility" TargetName="PART_Image" Value="Collapsed" />
                    </DataTrigger>
                    <Trigger Property="UIElement.IsMouseOver" Value="True">
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{Binding MouseOverBackground, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{Binding MouseOverBorderBrush, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF" />
                    </Trigger>
                    <Trigger Property="UIElement.IsKeyboardFocused" Value="True">
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{Binding FocusedBackground, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{Binding FocusedBorderBrush, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF" />
                    </Trigger>
                    <Trigger Property="ButtonBase.IsPressed" Value="True">
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{Binding PressedBackground, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{Binding PressedBorderBrush, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderBrush" TargetName="InnerBorder" Value="#00FFFFFF" />
                    </Trigger>
                    <Trigger Property="RibbonControlService.IsInControlGroup" Value="True">
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{Binding Ribbon.BorderBrush, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        <Setter Property="Border.BorderThickness" TargetName="OuterBorder" Value="0,0,1,0" />
                        <Setter Property="Border.CornerRadius" TargetName="OuterBorder" Value="0,0,0,0" />
                        <Setter Property="Border.CornerRadius" TargetName="InnerBorder" Value="0,0,0,0" />
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled" Value="False">
                        <Setter Property="UIElement.Opacity" TargetName="PART_Image" Value="0.5" />
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="#FF9E9E9E" />
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True">
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" />
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="#00FFFFFF" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="#00FFFFFF" />
                        <Setter Property="Border.CornerRadius" TargetName="OuterBorder" Value="0,0,0,0" />
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=TemplatedParent}}" Value="True" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=TemplatedParent}}" Value="True" />
                            <Condition Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="False" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue=false}" Value="True" />
                            <Condition Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="False" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Mode=Self}}" Value="True" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
                        <Setter Property="Border.CornerRadius" TargetName="OuterBorder" Value="0,0,0,0" />
                        <Setter Property="Border.BorderBrush" TargetName="InnerBorder" Value="#00FFFFFF" />
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsKeyboardFocused, RelativeSource={RelativeSource Mode=Self}}" Value="True" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
                        <Setter Property="Border.CornerRadius" TargetName="OuterBorder" Value="0,0,0,0" />
                        <Setter Property="Border.BorderBrush" TargetName="InnerBorder" Value="#00FFFFFF" />
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Mode=Self}}" Value="True" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Panel.Background" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
                        <Setter Property="Border.CornerRadius" TargetName="OuterBorder" Value="0,0,0,0" />
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsInControlGroup, RelativeSource={RelativeSource Mode=Self}}" Value="True" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Border.BorderBrush" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=Self}}" Value="False" />
                            <Condition Binding="{Binding Path=(SystemParameters.HighContrast)}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="TextElement.Foreground" TargetName="OuterBorder" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </MultiDataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

那里,Trigger行是罪魁祸首。

最终结果:

endresult

注意:我使用的是另一个图标。 :)

答案 1 :(得分:0)

您需要修改此按钮的模板。右键单击设计器并选择

  

修改模板 - &gt;编辑副本..

使整个按钮更宽或修改字体大小。另一种选择是将TextWrapping设置为None,但最终可能会出现裁剪文字。