简体   繁体   English

XAML文本块中的文本块和文本对齐方式

[英]XAML Textblocks and text alignment within the textblock

Is there something that I am missing? 有什么东西我错过了吗? We have a WPF project and using XAML for the layout. 我们有一个WPF项目并使用XAML进行布局。 In our C# we are populating a string of values using a ForEach loop and plugging that into a the TextBlock's text property AND we are adding a "\\n" so that each of the strings that get pushed back to us gets a line break at the end of the line. 在我们的C#中,我们使用ForEach循环填充一串值并将其插入到TextBlock的text属性中。我们正在添加一个“\\ n”,以便每个被推回给我们的字符串都会在队伍的尽头。

We have 2 TextBlocks next to each other. 我们有2个TextBlocks彼此相邻。 One TextBlock we are using as a Label and the next TextBlock we are using to get back the list of names. 我们用作Label的一个TextBlock和我们用来取回名称列表的下一个TextBlock。 But when the list of names populates, the text seems to stack on top of each other going upward not downward. 但是当名称列表填充时,文本似乎堆叠在彼此之上而不是向下。 If the List of Names (right textblock) textblock has 3 lines in it then the Label textblock (the left textblock) aligns with the bottom of the right textblock. 如果名称列表(右侧文本块)文本块中有3行,则标签文本块(左侧文本块)与右侧文本块的底部对齐。 The container of the two textblocks is actually another textblock like below. 两个文本块的容器实际上是另一个文本块,如下所示。 I have attached an image as well. 我也附上了一张照片。 Our child list is dynamic, so we can not set the child list or label to be a specific height. 我们的子列表是动态的,因此我们不能将子列表或标签设置为特定高度。

 <TextBlock x:Name="ChildNameText" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left" Padding="0" Margin="15,0,0,0">
                 <TextBlock x:Name="ChildLabelBold" FontWeight="Bold" Text="Child Label:" VerticalAlignment="Top" Margin="0,10" Padding="0,10,0,0"/>
                <TextBlock x:Name="ChildNameNormal" Text="Child Info" TextWrapping="Wrap" Width="Auto" Margin="0,10"/>
  </TextBlock>

Code Behind: 代码背后:

 if (dataSource.MyChildren != null && dataSource.MyChildren.Count > 0)
            {
                ChildLabelBold.Visibility = Visibility.Visible;
                ChildNameNormal.Visibility = Visibility.Visible;
                 ChildLabelBold.Text = "Child Name: ";

                //ChildNameText.Visibility = Visibility.Visible;
                string children = string.Empty;
                int childCount = dataSource.MyChildren.Count;
                int i = 1;
                foreach (var child in dataSource.MyChildren)
                {

                    children =  children + child.FirstName + " " + child.LastName + " - Date of Birth: " + child.DateOfBirth;

                    if (i < childCount)
                    {
                        children = children + "\n";
                    }
                    i++;
                }

                ChildNameNormal.Text = children;
                //ChildNameText.Text =  children;
             }
            else
            {
                ChildLabelBold.Visibility = Visibility.Collapsed;
                ChildNameNormal.Visibility = Visibility.Collapsed;
                //ChildNameText.Visibility = Visibility.Collapsed;
            }

What would be the solution to get these to align properly? 什么是让这些正确对齐的解决方案? The image below is basically what we are seeing. 下面的图片基本上就是我们所看到的。 We want the left BOLD text in the Textblock text to be at the top of the block. 我们希望Textblock文本中的左边BOLD文本位于块的顶部。

在此输入图像描述 Here: 这里:

EDIT: Per AMR's request, here is the full XAML. 编辑:根据AMR的要求,这是完整的XAML。

  <UserControl x:Class="MembershipApp.UserControls.ConfirmationWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="768" d:DesignWidth="1280" x:Name="ConfirmWindow">
<UserControl.Resources>
    <SolidColorBrush x:Key="brushWatermarkBorder" Color="White" />

</UserControl.Resources>
<Grid x:Name="MembershipConfirmationGrid" Background="#FF014A7F">

    <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Margin="-3,15,0,0" FontSize="42" Foreground="White" Text="Membership Confirmation" FontFamily="Segoe UI" TextAlignment="Center"/>
    <Grid x:Name="MembershipConfirmationInfoGrid" Margin="20,89,20,20" Width="Auto" Background="White">
        <StackPanel x:Name="PrimaryStack" HorizontalAlignment="Stretch" Width="Auto">
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="#424242"></Setter>
                    <Setter Property="Padding" Value="15,10,0,0"></Setter>
                    <Setter Property="FontSize" Value="18"></Setter>
                </Style>
            </StackPanel.Resources>

            <TextBlock x:Name="ConfirmationMessage" Text="Please confirm that the information below is correct and press 'Confirm'" FontWeight="Bold" Margin="15,15,15,20" Background="#FFD0F0FD" Padding="10" Foreground="#FF014A7F">
            </TextBlock>

            <!--TODO: Add Membership Type to the list in Code Behind-->
            <TextBlock x:Name="MembershipTypeText">
                <TextBlock x:Name="MembershipTypeLabelBold" FontWeight="Bold" Text="Membership Type Label:" Margin="0" Padding="0,10,0,0"></TextBlock>
                <TextBlock x:Name="MembershipTypeNormal" Text="Membership Type"></TextBlock>
            </TextBlock>


            <TextBlock x:Name="PrimaryNameText">
                <TextBlock x:Name="PrimaryLabelBold" FontWeight="Bold" Text="Primary Member Label:" Margin="0" Padding="0,10,0,0"></TextBlock>
                <TextBlock x:Name="PrimaryNameNormal" Text="Member Name"></TextBlock>
            </TextBlock>
            <TextBlock x:Name="SpouseNameText">
                <TextBlock x:Name="SpouseLabelBold" FontWeight="Bold" Text="Spouse Member Label:" Margin="0" Padding="0,10,0,0"></TextBlock>
                <TextBlock x:Name="SpouseNameNormal" Text="Member Name"></TextBlock>
            </TextBlock>

           <!-- <TextBlock x:Name="MembershipCategoryText" Text="Membership Category:">
            </TextBlock>-->

            <TextBlock x:Name="ChildNameText" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left" Padding="0" Margin="15,0,0,0">
                 <TextBlock x:Name="ChildLabelBold" FontWeight="Bold" Text="Child Label:" VerticalAlignment="Top" Margin="0,10" Padding="0,10,0,0"/>
                <TextBlock x:Name="ChildNameNormal" Text="Child Info" TextWrapping="Wrap" Width="Auto" Margin="0,10" VerticalAlignment="Top"/>
            </TextBlock>


            <TextBlock x:Name="AddressText" VerticalAlignment="Top">
                <TextBlock x:Name="AddressLabelBold" FontWeight="Bold" Text="Address Label:" VerticalAlignment="Top" Margin="0" Padding="0,10,0,0" HorizontalAlignment="Left"></TextBlock>
                <TextBlock x:Name="AddressNameNormal" Text="Address Info" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>

            </TextBlock>

            <TextBlock x:Name="Phone1Text">
                <TextBlock x:Name="Phone1LabelBold" FontWeight="Bold" Text="Home Phone Label:" VerticalAlignment="Top" Margin="0" Padding="0,10,0,0" HorizontalAlignment="Left"></TextBlock>
                <TextBlock x:Name="Phone1Normal" Text="Home Phone Info" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>
            </TextBlock>

            <TextBlock x:Name="Phone2Text" HorizontalAlignment="Left">
                <TextBlock x:Name="Phone2LabelBold" FontWeight="Bold" Text="Work Phone Label:" VerticalAlignment="Top" Margin="0" Padding="0,10,0,0" HorizontalAlignment="Left"></TextBlock>
                <TextBlock x:Name="Phone2Normal" Text="Work Phone Info" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>

            </TextBlock>

            <TextBlock x:Name="Phone3Text" HorizontalAlignment="Left">
                <TextBlock x:Name="Phone3LabelBold" FontWeight="Bold" Text="Cell Phone Label:" VerticalAlignment="Top" Margin="0" Padding="0,10,0,0" HorizontalAlignment="Left"></TextBlock>
                <TextBlock x:Name="Phone3Normal" Text="Cell Phone Info" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>
            </TextBlock>

            <TextBlock x:Name="EmailText" HorizontalAlignment="Left">
                <TextBlock x:Name="EmailLabelBold" FontWeight="Bold" Text="Email Label:" VerticalAlignment="Top" Margin="0" Padding="0,10,0,0" HorizontalAlignment="Left"></TextBlock>
                <TextBlock x:Name="EmailNormal" Text="Email Info" VerticalAlignment="Top" TextWrapping="Wrap" HorizontalAlignment="Left"></TextBlock>
            </TextBlock>
        </StackPanel>

    </Grid>
    <Grid x:Name="CofirmationButtons" Margin="0,89,20,30" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="600" Height="80">
        <StackPanel x:Name="ButtonGroup1" Margin="0,0,0,15" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center">
            <Button x:Name="BtnBack" Content="Back" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="80" Height="47" Margin="10,0" Background="#FF5B5B5B"/>
            <Button x:Name="BtnApprove" Content="Confirm" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="200" Height="47" Margin="10,0"/>
        </StackPanel>
    </Grid>
    <StackPanel x:Name="MembershipPickStackPanel" Margin="10,50,10,0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    <TextBlock HorizontalAlignment="Left" Margin="15,58,0,0" TextWrapping="Wrap" Text="No. of Children:" VerticalAlignment="Top" Width="96" Visibility="Hidden"/>
    <ComboBox x:Name="NumberOfChildrenDDL" HorizontalAlignment="Left" Margin="111,51,0,0" VerticalAlignment="Top" Width="51" Visibility="Hidden"/>
</Grid>

Your code is completely wrong. 你的代码是完全错误的。 Delete all that and start all over. 删除所有内容并从头开始。

You must NOT manipulate UI elements in code in WPF. 您不能在WPF中的代码中操作UI元素。

<Window x:Class="MiscSamples.LabelledList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LabelledList" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Child Names:" VerticalAlignment="Top" Margin="2"
                   FontWeight="Bold" />

        <ItemsControl ItemsSource="{Binding}" Margin="2" DisplayMemberPath="DisplayName"
                      Grid.Column="1"/>
    </Grid>
</Window>

Code Behind: 代码背后:

public partial class LabelledList : Window
{
    public LabelledList()
    {
        InitializeComponent();

        DataContext = Enumerable.Range(0, 10)
                                .Select(x => new SomeClass() { DisplayName = "Child" + x.ToString()})
                                .ToList();
    }
}

Data Item: 数据项:

public class SomeClass
{
    public string DisplayName { get; set; }
}

Result: 结果:

在此输入图像描述

You need to set the vertical alignment to top in the childNameNormal textblock its not set in child normal 您需要在childNameNormal文本块中将垂直对齐设置为top,而不是在child normal中设置

In this one 在这一个

    <TextBlock x:Name="ChildNameNormal" Text="Child Info" TextWrapping="Wrap" Width="Auto" Margin="0,10"/>
  </TextBlock>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM