In a horizontal listbox how do I align items to the top?
I have ran out of ideas of where to stick a VerticalAlignment="Top".
<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; }
}
}