【问题标题】:XAML Textblocks and text alignment within the textblockXAML 文本块和文本块内的文本对齐
【发布时间】:2013-05-01 19:07:36
【问题描述】:

有什么我遗漏的吗?我们有一个 WPF 项目并使用 XAML 进行布局。在我们的 C# 中,我们使用 ForEach 循环填充一串值并将其插入到 TextBlock 的文本属性中,并且我们正在添加一个“\n”,以便每个被推回给我们的字符串在行尾。

我们有 2 个相邻的 TextBlock。我们将一个 TextBlock 用作标签,而下一个 TextBlock 我们用于取回名称列表。但是当名称列表填充时,文本似乎堆叠在一起向上而不是向下。如果名称列表(右侧文本块)文本块中有 3 行,则标签文本块(左侧文本块)与右侧文本块的底部对齐。两个文本块的容器实际上是另一个文本块,如下所示。我也附上了一张图片。我们的子列表是动态的,所以我们不能将子列表或标签设置为特定的高度。

 <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>

代码背后:

 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;
            }

让这些正确对齐的解决方案是什么?下图基本上就是我们所看到的。我们希望 Textblock 文本中的左侧 BOLD 文本位于块的顶部。

这里:

编辑:根据 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>

【问题讨论】:

  • 老兄,您的代码完全错误。贴一张你需要的截图,我可以告诉你在 WPF 中实现它的正确方法。
  • 发布了我们现在拥有的图像,任何清理或其他建议都会很棒。我们希望 BOLD 标签位于左侧文本块的顶部。
  • 这段代码让我头疼。请截图。

标签: c# xaml


【解决方案1】:

您的代码完全错误。删除所有内容并重新开始。

您不得在 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>

代码背后:

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

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

数据项:

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

结果:

【讨论】:

  • '您不得在 WPF 的代码中操作 UI 元素。'兄弟传道!
  • @AMR 不管你怎么称呼我,但我的代码比我在人们尝试以 winforms 方式使用 WPF 时看到的许多可怕的 hack 更干净、更简单、更易于维护。
  • 看起来好多了。感谢您的反馈。我将把它带给这里的其他开发人员。我只是在学习其中的大部分内容,所以其中一些是我的代码。还有一些我们以前从未真正做过的事情。这就是 StackOverflow 和其他类似网站的原因。再次感谢!
  • 哈哈@HighCore(我同意你的观点)
【解决方案2】:

您需要在 childNameNormal 文本块中将垂直对齐设置为顶部,它未在子法线中设置

在这个

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

【讨论】:

  • 您是否进行了干净的重建?
  • 是的,我们有另一个文本块基本上做同样的事情。清理、重建并运行它。
  • 您能否发布您的完整 xaml 文件,以便我查看它周围的其他内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多