开发者

Bug or User error? Unable to set TextBlock's VerticalAlignment property to Center

Hmmm. . . I'm losing my mind. . . or am I?

I'm creating a WPF app for some basic data entry. I'm using textblocks to label the textboxes but ran into a snag. Why can't I vertically center the textblocks? I can't change the vertical alignment at all. Regardless of what I value I set on the property, the textblocks remain at the top. I want them centered! I can change the horizontal alignment no problem.

The contents of the XAML file, including the styles, are included below.

<UserControl xmlns="http://schemas.microsoft.com/wi开发者_运维百科nfx/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>


I am not 100% sure why it is not centering, probably has to do with the height set on the elements, but what you can do is apply a padding to the TextBlocks to get them centered with the TextBoxes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜