Creating a WP7 app: Listbox items of the same width

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>

About these ads

About timdams
C#, .NET, Microsoft, security, .... Read more on : http://timdams.com/

6 Responses to Creating a WP7 app: Listbox items of the same width

  1. You can achieve your goal simplier.

  2. Style TargetType=”ListBoxItem”
    Setter Property=”HorizontalAlignment” Value=”Stretch”
    Setter Property=”HorizontalContentAlignment” Value=”Stretch”
    /Style

  3. Pingback: Using full container width for ListBoxItem » Saintchubs.com

  4. Pingback: DotNetShoutout

  5. Pingback: Creating a WP7 app: Listbox items of the same... | .NET, C# | Syngu

  6. xin says:

    greate job, I like it~

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 327 other followers

%d bloggers like this: