2015-12-30 30 views
0

我試圖中心在中心按鈕上的文字內容,在我的通用的應用程序,但是這是我得到:中心按鈕上的文字內容在一個通用的應用程序

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 =「中心」,但像你看到的,我沒有得到我想要 感謝您的幫助,結果

種我的按鈕樣式:

<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

我們看不到所有的代碼......你在做什麼但是tontopStyle?當我嘗試你的代碼片段時,它正常工作。 – Depechie

回答

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套件\ 10 \設計時\ CommonConfiguration \中性\ UAP \ 10.0.10240.0 \通用\通用.xaml

+1

好點,巴特。對於那些不想挖掘generic.xaml的人,你可以通過msdn中的控制來瀏覽... https://msdn.microsoft.com/zh-cn/library/windows/apps/mt299122.aspx –

+0

感謝馬克爵士的鏈接 – user3821206

相關問題