2014-01-21 91 views
1

我正在用C#製作Windows Phone 8應用程序,我試圖用多個列表框來顯示XML到帶有幾個列表框的數據透視頁面的輸出,唯一的障礙是我的方式在配料框中的項目,他們似乎是這樣的:列表框中的項目對齊

  • 食鹽

當我真正希望他們能夠出現這樣的

  • 食鹽
  • 1撮

基本上每個項目將出現在單獨一行,並通過與疊板亂搞我似乎無法得到任何東西出現在同一行,這是我正在使用的xaml代碼:

  <Grid x:Name="ContentPanel1" Grid.Row="1" Margin="12,0,12,0"> 
       <ListBox Height="600" HorizontalAlignment="Left" Margin="21,6,0,0" Name="ingredientsList" VerticalAlignment="Bottom" Width="413"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
           <StackPanel> 
            <TextBlock x:Name="lbl_Name" TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0" Text="{Binding IngredientName}" Foreground="White" FontWeight="Bold"/> 
            <TextBlock x:Name="lbl_Quantity" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientQuantity}" Foreground="White"/> 
            <TextBlock x:Name="lbl_Unit" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientUnit}" Foreground="White"/> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 

任何人都可以提出修復建議嗎?提前致謝!

回答

1

StackPanel堆棧包含垂直項目。你可以使用一個單一的TextBlock有兩個運行要素:

<StackPanel> 
    <TextBlock x:Name="lbl_Name" TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0" Text="{Binding IngredientName}" Foreground="White" FontWeight="Bold"/> 
    <TextBlock x:Name="lbl_Quantity" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Foreground="White"> 
     <Run Text="{Binding IngredientQuantity}" /> 
     <Run Text="{Binding IngredientUnit}" /> 
    </TextBlock> 
</StackPanel> 
2

如果是我,我會窩在你的數據模板的另一個堆疊面板

<DataTemplate> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock x:Name="lbl_Name" TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0" Text="{Binding IngredientName}" Foreground="White" FontWeight="Bold"/> 
      <StackPanel Orientation="Horizontal">          
       <TextBlock x:Name="lbl_Quantity" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientQuantity}" Foreground="White"/> 
       <TextBlock x:Name="lbl_Unit" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientUnit}" Foreground="White"/> 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
+0

+ 1我寫了相同的答案,丟棄後,你的:) –

+0

LoL我贏了這一輪先生... –

1

可能是這樣

<Grid x:Name="ContentPanel1" Grid.Row="1" Margin="12,0,12,0"> 
        <ListBox Height="600" HorizontalAlignment="Left" Margin="21,6,0,0" Name="ingredientsList" VerticalAlignment="Bottom" Width="413"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
            <StackPanel> 
             <TextBlock x:Name="lbl_Name" TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0" Text="{Binding IngredientName}" Foreground="White" FontWeight="Bold"/> 
             <StackPanel Orientation="Horizontal"> 
             <TextBlock x:Name="lbl_Quantity" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientQuantity}" Foreground="White"/> 
             <TextBlock x:Name="lbl_Unit" TextWrapping="Wrap" FontSize="20" Margin="10,0,0,0" Text="{Binding IngredientUnit}" Foreground="White"/> 
             </StackPanel> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </Grid> 
1

使用StackPanel屬性Orientation =「Horizo​​ntal」

<Grid x:Name="ContentPanel1" Grid.Row="1" Margin="12,0,12,0"> 
          <ListBox Height="600" HorizontalAlignment="Left" Margin="21,6,0,0" Name="ingredientsList" VerticalAlignment="Bottom" Width="413"> 
           <ListBox.ItemTemplate> 
            <DataTemplate> 
            <StackPanel> 
            <TextBlock x:Name="lbl_Name" TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0" Text="{Binding IngredientName}" Foreground="White" FontWeight="Bold"/> 
            <StackPanel Orientation="Horizontal">           
            <TextBlock x:Name="lbl_Quantity" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientQuantity}" Foreground="White"/> 
            <TextBlock x:Name="lbl_Unit" TextWrapping="Wrap" FontSize="20" Margin="0,0,0,0" Text="{Binding IngredientUnit}" Foreground="White"/> 
            </StackPanel> 
            </StackPanel>  
            </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </Grid> 
+0

這將水平對齊所有項目! – christianliebel

+0

不是所有的項目,給出的答案是正確的@chliebel –

+0

編輯完成後... – christianliebel