WPF - 无法将文本对齐到按钮的中心

时间:2014-08-25 10:19:28

标签: wpf

我有以下自定义按钮,效果很好:

MyCustomButton

<Button.Resources>
    <Style TargetType="Button" x:Key="MyCustomButton">
        <Setter Property="Foreground" Value="#FFFFFFFF"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter Property="Padding" Value="20,10,20,10"/>
        <Setter Property="Margin" Value="0,0,5,5"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" MaxWidth="{TemplateBinding MaxWidth}" MaxHeight="{TemplateBinding MaxHeight}" ClipToBounds="True">
                        <Border x:Name="border" CornerRadius="4" BorderBrush="transparent" BorderThickness="0">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#FF0088D9" Offset="0"/>
                                    <GradientStop Color="#FF007BC4" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <TextBlock x:Name="content" Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Height="Auto">
                                <Run x:Name="icon" Text="{Binding Path=Icon, RelativeSource={RelativeSource TemplatedParent}}"/>
                                <Run x:Name="text" Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}"/>
                            </TextBlock>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Resources>

然而,我不能,不可能,它内部的文本(通过&#34;运行&#34;标记为&#34;图标&#34;和#34;文本&#34;)的标记。保持居中对齐。诀窍是什么?提前谢谢!

0 个答案:

没有答案