将按钮文本内容置于通用应用程序中心

时间:2015-12-30 10:12:21

标签: xaml win-universal-app windows-10-universal

我正试图在我的通用应用中将按钮的文本内容集中在中心,但这就是我得到的:

enter image description here

这是我的代码:

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

1 个答案:

答案 0 :(得分:2)

你付出了太多的努力,而ButtonMenuStyle很可能会让你的布局变得混乱。默认情况下,Button的内容垂直居中。

我已经抛弃了所有不必要的对齐方式,不必要的边框(您甚至可以删除内部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>

enter image description here

编辑:当你发布你的风格时,我调查了它们。我强烈建议不要使用它们(因为它们会破坏一些东西),但是如果你真的想要使用它们,你也应该改变你的ContentPresenter

<ContentPresenter x:Name="Content" VerticalContentAlignment="Center" Content="{TemplateBinding Content}"/>

如果要修改按钮控制模板,请查看以下文件(第2147行)中的默认模板作为起点:

  

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