开发者

Silverlight Listbox with variable height fitting to content

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜