2009-06-24 13 views
0

横のリストボックスでアイテムを一番上にどのように揃えますか?横のリストボックスでアイテムを一番上にどのように揃えますか?

私は、VerticalAlignment = "Top"をどこに貼り付けるかのアイディアが不足しています。あなたは、リストボックスにVerticalContentAlignmentを設定する必要が

<Window.Resources> 
    <DataTemplate DataType="{x:Type l:MyType}"> 
     <Grid VerticalAlignment="Top"> 
      <TextBlock VerticalAlignment="Top" Text="{Binding MyValue}" Background="Yellow"/> 
     </Grid> 
    </DataTemplate>  
</Window.Resources> 

<Grid> 
    <ListBox Name="listBox" ItemsSource="{Binding}" VerticalAlignment="Top" > 

     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" VerticalAlignment="Top"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <ListBoxItem Content="{Binding}" VerticalAlignment="Top" VerticalContentAlignment="Top"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

using System.Windows; 

namespace WpfApplication5 { 
    public partial class Window1 :Window { 
     public Window1() { 
      InitializeComponent(); 
      this.listBox.ItemsSource = new MyType[] { 
       new MyType{ MyValue = "Tall\nItem" }, 
       new MyType{ MyValue = "I want this aligned to the top" } }; 
     } 
    } 

    public class MyType { 
     public string MyValue { get; set; } 
    } 
} 

答えて

2

ものが設定されると、あなたがそこに持っている他のすべての整列が同様に除去することができます。

.... 
<ListBox Name="listBox" ItemsSource="{Binding}" VerticalContentAlignment="Top" > 
.... 

あなたのListBoxのVerticalContentAlightmentのデフォルトは「センター」ですので、あなたが他の場所でVerticalAlignmentを設定していても、それだけではまだ中心の代わりに、随分かに置いたListBoxItemsの最上部に揃えましたトップ。 VerticalContentAlignmentをStretchに設定すると、他のVerticalAlignment = "Top"宣言が機能します。

+0

Arrrrggです。おかげさまで – jyoung

関連する問題