Question

Dans le XAML suivant, le mot de centres "test" horizontalement mais pas verticalement.

Comment puis-je obtenir à centrer verticalement?

<Window x:Class="TestVerticalAlign2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
    Title="Window1" Height="768" Width="1024">
    <DockPanel LastChildFill="True">
        <Slider x:Name="TheSlider"
                DockPanel.Dock="Left"
                Orientation="Vertical"
                HorizontalAlignment="Center"
                HorizontalContentAlignment="Center"
                Minimum="0"
                Maximum="10"
                Cursor="Hand"
                Value="{Binding CurrentSliderValue}"
                IsDirectionReversed="True"
                IsSnapToTickEnabled="True"
                Margin="10 10 0 10"/>
        <Border DockPanel.Dock="Right" Background="Beige"
                Padding="10"
                Margin="10"
                CornerRadius="5">
            <StackPanel Height="700">
                <TextBlock
                    Text="Test"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="200" x:Name="TheNumber"/>

            </StackPanel>
        </Border>
    </DockPanel>
</Window>
Était-ce utile?

La solution

A StackPanel, peu importe comment vous étirer, s'effondrer autour des enfants. vous ne pouvez pas le faire grandir plus que cela. Au fond, que "height = 700" ne vous aide pas.

soit mis VerticalAlignment sur le StackPanel au « centre » pour que le StackPanel va dans le centre de la DockPanel ... ou supprimer le StackPanel tout à fait et mis VerticalAlignment = « center » sur le TextBlock.

Autres conseils

Il semble que je posé cette question il y a 10 mois , je suis arrivé le scénario ci-dessus pour travailler en remplaçant le StackPanel DockPanel LastChildFill = True comme ceci:

<DockPanel LastChildFill="True">
    <TextBlock
        DockPanel.Dock="Top"
        Text="Test"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        FontSize="200" x:Name="TheNumber"/>
</DockPanel>

Je suis tombé sur ce qui semble fonctionner parfaitement:

<Grid>
    <TextBlock Text="My Centered Text"
               TextAlignment="Center" 
               VerticalAlignment="Center"/>
</Grid>

La grille assure que la zone de texte unique en son sein remplit la cellule d'isolement dans la grille et la VerticalAlignment du TextBlock veille à ce que le texte soit centrée que.

Il suffit de positionner / aligner votre texte horizontalement mais vous avez besoin (l'extrait ci-dessus, il centre dans cet axe également, mais en changeant cela ne modifie pas le centrage vertical).

A l'intérieur du StackPanel qui entoure le TextBlock, consultez VerticalContentAlignment.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top