在按钮上设置字体大小ContentPresenter更改文本的垂直对齐方式 - XAML

时间:2014-08-22 17:25:29

标签: c# wpf xaml wpf-controls togglebutton

我有一个ToggleButton自定义控件,可在切换时更改文本和大小。我可以更改/设置字体并更改背景/前景属性,它可以正常工作。

但是一旦我设置ContentPresenter的FontSize属性,按钮中的文本就会错位!特别是垂直位置似乎是关闭的(在按钮中向下移动)

我无法弄清楚造成这种情况的原因。如果我没有设置fontsize,一切都按预期工作。这是我的按钮。

<Style x:Key="PlusSelectedToggleButton" TargetType="{x:Type ToggleButton}" BasedOn="{x:Null}">
    <Setter Property="Background" Value="#FF3498DB" />
    <Setter Property="BorderBrush" Value="DarkGray" />
    <Setter Property="Padding" Value="1"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">           
                <Border x:Name="Border" Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="4">
                    <ContentPresenter x:Name="ContentPresenter" RecognizesAccessKey="True"
                                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

                                          />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" Value="Black" TargetName="Border" />
                        <Setter Property="BorderBrush" Value="White" TargetName="Border" />
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="TextBlock.Foreground" Value="Black" TargetName="ContentPresenter" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="true">
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Content" Value="Selected" />
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="TextBlock.Foreground" Value="DarkGray" TargetName="ContentPresenter" />
                        <Setter Property="BorderBrush" Value="Transparent" />
                        <Setter Property="Width" Value="75" TargetName="Border" />
                        <Setter Property="Height" Value="40" TargetName="Border" />
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Content" Value="+"/>
                        <Setter Property="TextBlock.FontSize" Value="50" TargetName="ContentPresenter"/>
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                        <Setter Property="Background" Value="#FF3498DB" />
                        <Setter Property="Width" Value="40"/>
                        <Setter Property="Height" Value="40"/>
                        <Setter Property="TextBlock.Foreground" Value="White" TargetName="ContentPresenter" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

更新:我让它开始工作,但我仍然不完全明白为什么必须这样做。

   <Trigger Property="IsChecked" Value="False">
           <Setter Property="Content" Value="+"/>
           <Setter Property="TextBlock.Foreground" Value="White" TargetName="ContentPresenter" />
           <Setter Property="TextBlock.FontSize" Value="27" TargetName="ContentPresenter"/>
           <Setter Property="TextBlock.FontWeight" Value="Thin"></Setter>
           <Setter Property="TextBlock.Height" Value="40" TargetName="ContentPresenter"/>
           <Setter Property="VerticalContentAlignment" Value="Center"/>
           <Setter Property="Background" Value="#FF3498DB" />
           <Setter Property="Width" Value="35"/>
           <Setter Property="Height" Value="35"/>                            
</Trigger>

我必须将 TextBlock.Height 属性设置为更大的值(甚至大于按钮高度)才能使文本垂直居中。水平地它停留在同一个地方。我不确定为什么它会像这样。

0 个答案:

没有答案