2013-05-01 97 views
1

有沒有我丟失的東西?我們有一個WPF項目並使用XAML進行佈局。在我們的C#中,我們使用ForEach循環填充一串值並將其插入到TextBlock的文本屬性中,並且我們添加了一個「\ n」,以便每個返回給我們的字符串在隊伍的盡頭。文本塊中的XAML文本塊和文本對齊

我們有2個TextBlocks彼此相鄰。我們將使用一個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文本位於塊的頂部。

enter image description here 這裏:

編輯:每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

哥們你的代碼是完全錯誤的。發佈你需要的截圖,我可以告訴你在WPF中實現它的正確方法。 – 2013-05-01 19:10:53

+0

發佈了我們現在擁有的圖片,任何清理或其他建議都會很棒。我們希望BOLD標籤位於左側文本塊的頂部。 – ClosDesign 2013-05-01 19:12:20

+0

這段代碼讓我頭疼。請將屏幕截圖。 – 2013-05-01 19:13:00

回答

0

您需要設置的垂直對齊的childNameNormal頂部文本塊它不是兒童正常

設置在這一個

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

我其實已經試過了。這仍然沒有奏效。 – ClosDesign 2013-05-01 19:15:19

+0

你做了一個乾淨的重建? – 2013-05-01 19:15:46

+0

是的,我們有另一個文本塊基本上做同樣的事情。清理,重建,並運行它。 – ClosDesign 2013-05-01 19:18:26

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

結果:

enter image description here

+0

'您不得在WPF代碼中操作UI元素。'把它推薦給兄弟! – 2013-05-01 19:26:19

+0

@AMR給我打電話,不管你會怎樣,但是我的代碼比人們試圖以winforms方式使用WPF時見過的許多可怕的黑客更乾淨,更簡單,更可靠。 – 2013-05-01 19:27:35

+0

看起來好多了。感謝您的反饋。我會把這件事帶給這裏的其他開發者。我只是學習了大部分,所以有些是我的代碼。還有一些我們以前從未真正做過的東西。這就是爲什麼StackOverflow和其他這樣的網站。再次感謝! – ClosDesign 2013-05-01 19:29:32