我有什么遗失的东西吗?我们有一个WPF项目并使用XAML进行布局。在我们的C#中,我们使用ForEach循环填充一串值并将其插入到TextBlock的text属性中。我们正在添加一个“\ n”,以便每个被推回给我们的字符串都会在结束了。
我们彼此相邻有2个TextBlock。我们用作Label的一个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>
答案 0 :(得分:3)
您的代码完全错误。删除所有内容并从头开始。
您不得在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; }
}
结果:
答案 1 :(得分:0)
你需要在childNameNormal文本块中将垂直对齐设置为top,而不是在child normal中设置
在这一个
<TextBlock x:Name="ChildNameNormal" Text="Child Info" TextWrapping="Wrap" Width="Auto" Margin="0,10"/>
</TextBlock>