$ cat "

Getting a ListBoxItem to stretch horizontally in Silverlight

"

When you specify an ItemTemplate for a ListBox in silverlight, you can't get it to stretch horizontally in an easy way. Setting the HorizontalAlignment of the container in the template to stretch has no effect. What you have to do is to specify an ItemContainerStyle for the ListBox and set the HorizontalContentAlignment to Stretch:

<ListBox ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser, Mode=TwoWay}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="#CECECE" Padding="5" CornerRadius="3"
Background="{Binding Background}">
<!-- Your stuff here -->
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Written by Erik Öjebo 2011-11-29 08:05

    Comments