Mobile Zone is brought to you in partnership with:

Tim Dams is a teacher at the Artesis University College in Antwerp, Belgium where he is mainly involved in software engineering and programming courses. In his spare time he likes to write Silverlight, WPF and Windows Phone 7 applications and blog about the things he learns in the process. Tim is a DZone MVB and is not an employee of DZone and has posted 19 posts at DZone. You can read more from them at their website. View Full User Profile

Creating a Windows Phone app: Listbox items of the same width

01.22.2012
| 2942 views |
  • submit to reddit

I love databound listboxes in WP7. I do. I really do. However, every time I come to the part of making a nice template for each item (i.e. shiny ugly orange border, etc.) I fail in having items of equal size. This is usally what I produce: 



Resulting in:

In WPF, the trick is to set the HorizontalAlignment=”Stretch” property of the Listbox. However, apparently there still some internal bug in the ListBox control, which results in the ListBox simply ingoring that setting.

The solution however is, luckily, provided by Microsoft’s Peter Torr in a reply in here.

By simply adding the ItemContainerStyle as described in the post , to the ListBox we get the result we are looking for:

Resulting in ..hooray:

Update: As Alex Sorokoletov was so kindly to cmment (thanks for that), there’s even a simplier solution:

<ListBox.ItemContainerStyle>
<Style TargetType=”ListBoxItem”>

<Setter Property=”HorizontalAlignment” Value=”Stretch” />
<Setter Property=”HorizontalContentAlignment” Value=”Stretch”/>

</Style>
</ListBox.ItemContainerStyle>

Source:  http://timdams.wordpress.com/2011/11/30/creating-a-wp7-app-listbox-items-of-the-same-width/


Published at DZone with permission of Tim Dams, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)