Xaml 在通用应用程序中将按钮文本内容居中

Xaml 在通用应用程序中将按钮文本内容居中,xaml,win-universal-app,windows-10-universal,Xaml,Win Universal App,Windows 10 Universal,在我的通用应用程序中,我试图将按钮的文本内容居中,但我得到的结果是: 这是我的代码: <StackPanel Orientation="Vertical" > <Border VerticalAlignment="Center" Height="40" > <Button Background="Transparent" Content="Resulta

在我的通用应用程序中,我试图将按钮的文本内容居中,但我得到的结果是:

这是我的代码:

<StackPanel Orientation="Vertical"  >
                        <Border  VerticalAlignment="Center" Height="40"  >
                            <Button  Background="Transparent" Content="Resultats" FontSize="16" VerticalAlignment="Stretch" Foreground="#727271"  x:Name="res" Style="{Binding Source={StaticResource ButtontopStyle}}" HorizontalAlignment="Stretch"  Click="res_Click" HorizontalContentAlignment="Left"  VerticalContentAlignment="Center" Height="40"/>
                        </Border>
                        <StackPanel x:Name="stackVisible" Orientation="Vertical" >
                        <Border Margin="0" >
                                <Button  Background="Transparent" Content="Tous les résultats" FontSize="14" VerticalAlignment="Top" Foreground="#727271" Margin="0"  x:Name="ttres" Style="{Binding Source={StaticResource ButtonMenuStyle}}"  HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Padding="10" VerticalContentAlignment="Center" Height="40"/>
                        </Border>
                        <Border>
                                <Button Background="Transparent" Content="Recherches Avancées" FontSize="14" VerticalAlignment="Top" Foreground="#727271" Margin="0"  x:Name="rechavan"  HorizontalAlignment="Stretch" Style="{Binding Source={StaticResource ButtonMenuStyle}}" HorizontalContentAlignment="Left" Padding="10" VerticalContentAlignment="Center" Height="40"/>
                        </Border>
                        </StackPanel>
          </StackPanel>

我尝试使用VerticalContentAlignment=“Center”,但正如您所看到的,我没有得到我想要的结果 谢谢你的帮助

我的按钮样式:

<Style  x:Key="ButtonMenuStyle" TargetType="Button">
            <Setter Property="Foreground" Value="#e6e6e6"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Border" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Border" />
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid>
                                <Rectangle x:Name="Border" Stroke="Transparent" Fill="Transparent" Margin="0"/>
                                <ContentPresenter x:Name="Content"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style  x:Key="ButtontopStyle" TargetType="Button">
            <Setter Property="Foreground" Value="#e6e6e6"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Button" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Button" />
                                            <ColorAnimation Duration="0" To="#e6e6e6" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid>
                                <Rectangle x:Name="Button" Stroke="Transparent" Fill="Transparent" Margin="0"/>
                                <ContentPresenter x:Name="Content"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

您在这方面投入了太多的精力,很可能是
按钮的菜单样式
把您的布局搞乱了。默认情况下,
按钮的内容是垂直居中的

我已经扔掉了所有不必要的对齐,不必要的边框(你甚至可以删除内部的
StackPanel
)。我还将一个按钮上的字体大小更改为8,以显示多个大小,以证明它们都正确对齐

<StackPanel Orientation="Vertical">
    <Button Background="Transparent" Content="Resultats" FontSize="16" VerticalAlignment="Stretch" Foreground="#727271" HorizontalAlignment="Stretch"  HorizontalContentAlignment="Left" Height="40"/>
    <StackPanel Orientation="Vertical" >
        <Button Background="Transparent" Content="Tous les résultats" FontSize="14" Foreground="#727271" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Height="40"/>
        <Button Background="Transparent" Content="Recherches Avancées" FontSize="8" Foreground="#727271" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Height="40" />
    </StackPanel>
</StackPanel>
如果要修改按钮控件模板,请查看以下文件(第2147行)中的默认模板作为起点:

C:\Program Files(x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic\Generic.xaml


我们看不到所有的代码。。。你的钮扣样式是什么?当我尝试你的代码片段时,它很有效。很好,巴特。对于那些不想深入研究generic.xaml的用户,您也可以通过msdn中的控件进行浏览。。。
<ContentPresenter x:Name="Content" VerticalContentAlignment="Center" Content="{TemplateBinding Content}"/>