错误或用户错误?无法将TextBlock的VerticalAlignment属性设置为Center

时间:2009-10-08 17:52:38

标签: xaml

嗯。 。 。我正在失去理智。 。 。还是我?

我正在为一些基本数据输入创建一个WPF应用程序。我正在使用文本块来标记文本框,但遇到了麻烦。为什么我不能垂直居中文本块?我根本无法改变垂直对齐方式。无论我在属性上设置了什么,文本块都保留在顶部。我希望他们居中!我可以改变水平对齐没问题。

下面包含XAML文件的内容,包括样式。

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="SL3_ContactEntry.MainPage"
             Width="500"
             Background="#FF99DF52">

    <UserControl.Resources>
        <Style x:Key="MyTextBlockStyle"
               TargetType="TextBlock">
            <Setter Property="FontSize"
                    Value="12" />
            <Setter Property="Margin"
                    Value="2" />
            <Setter Property="Width"
                    Value="Auto" />
            <Setter Property="Height"
                    Value="28" />
            <Setter Property="HorizontalAlignment"
                    Value="Right" />
            <Setter Property="VerticalAlignment"
                    Value="Center" />
        </Style>
        <Style x:Key="MyTextBoxStyle"
               TargetType="TextBox">
            <Setter Property="FontSize"
                    Value="12" />
            <Setter Property="Margin"
                    Value="2" />
            <Setter Property="Width"
                    Value="Auto" />
            <Setter Property="Height"
                    Value="28" />
        </Style>
        <Style x:Key="MyButtonStyle"
               TargetType="Button">
            <Setter Property="FontSize"
                    Value="12" />
            <Setter Property="Margin"
                    Value="2" />
            <Setter Property="Width"
                    Value="100" />
            <Setter Property="Height"
                    Value="33" />
        </Style>
    </UserControl.Resources>

    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.81*" />
            <RowDefinition Height="0.19*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical"
                    Grid.Column="0"
                    Margin="5">
            <TextBlock Name="FName"
                       Text="First Name"
                       Style="{StaticResource MyTextBlockStyle}" />
            <TextBlock Name="LName"
                       Text="Last Name"
                       Style="{StaticResource MyTextBlockStyle}" />
            <TextBlock Name="StreetAddress"
                       Text="Street Address"
                       Style="{StaticResource MyTextBlockStyle}" />
            <TextBlock Name="City"
                       Text="City"
                       Style="{StaticResource MyTextBlockStyle}" />
            <TextBlock Name="State"
                       Text="State"
                       Style="{StaticResource MyTextBlockStyle}" />
            <TextBlock Name="ZipCode"
                       Text="Zip Code"
                       Style="{StaticResource MyTextBlockStyle}" />
        </StackPanel>
        <StackPanel Orientation="Vertical"
                    Grid.Column="1"
                    Margin="5">
            <TextBox Name="txtFName"
                     Style="{StaticResource MyTextBoxStyle}" />
            <TextBox Name="txtLName"
                     Style="{StaticResource MyTextBoxStyle}" />
            <TextBox Name="txtStreetAddress"
                     Style="{StaticResource MyTextBoxStyle}" />
            <TextBox Name="txtCity"
                     Style="{StaticResource MyTextBoxStyle}" />
            <TextBox Name="txtState"
                     Style="{StaticResource MyTextBoxStyle}" />
            <TextBox Name="txtZipCode"
                     Style="{StaticResource MyTextBoxStyle}" />
            <Button Content="OK"
                    Style="{StaticResource MyButtonStyle}" />
        </StackPanel>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

我不是百分之百确定它为什么不居中,可能与元素上设置的高度有关,但你可以做的是对TextBlocks应用填充以使它们与TextBoxes居中。