How do I configure the listbox in Silverlight 开发者_如何学运维to have variable height ?
And this is the answer: :)
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Which height are you referring to? The control itself? Or the items inside the ListBox?
If you are referring to the height of the control, by default it is set to "stretch" therefore it will expand to with respect to its parent:
<Grid Name="LayoutRoot" Height="500">
<ListBox />
</Grid>
If you want the ListBox to expand without relating to its parent either: set its VerticalAlignment="Top":
<Grid Name="LayoutRoot" Height="500">
<ListBox VerticalAlignment="Top" />
</Grid>
or:
<Grid Name="LayoutRoot" Height="500">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox VerticalAlignment="Top" Grid.Row="0" />
</Grid>
The grid will expand with respect to its contents no its parent. you can also use converters bound to a height property or set the height in the xaml's codebehind.
If you are talking about the contents of the ListBox, create an ItemTemplate. An ItemTemplate will dictate how each of the contents will be displayed.
You can also set the itemspanel, this will dictate how the list will be displayed. You can use Stackpanel/WrapPanel.
精彩评论