My ListBox items WILL NOT stretch to maximum widths? WHY NOT?!
Wednesday, November 02, 2011 10:24 PM
Are you trying to make your items in your ListBox stretch out to the maximum width of your container? And it’s JUST NOT WORKING for you? I have two things you might try (and hopefully one will work).
For the last couple of nights, I’ve been FIGHTING with my WP7 DevTeach Ottawa 2011 app to show Presenters “nicely.” Ok, ok, so I’m NOT a graphic designer, but I think my UI works and looks nice. But I’m fighting with forcing the content of my ListBox items to be the maximum width of the container. Here’s what I’m talking about.
Do you see the spaces on the far right? Doesn’t look very nice, ugly in my opinion actually! And I DID set the contents to a Grid, set the 2nd column width to be the Star (XAMLs way of saying 100%), blah blah blah…….I DID all of that standard stuff! But I still get the ugly looking thing above!
Turns out I’m not the only one. I asked Laurent Duveau about this, and he quickly replied with trying to set the HorizontalContentAlignment = “Stretch”. hhhmm he’s seen this, done that and figured out possible solution.
Unfortunately this didn’t solve my problem...well, not exactly! The thing about MVPs is, sometimes, if they don’t give you the right answer, they DO give you enough to go FIND the answer! Which is exactly what I did! I found another link which helped me fix my problem via a different mechanism, using the ListBox.ItemContainerStyle within the ListBox. (sorry for the lack of xaml/xml to cut’n’paste, but my blog engine doesn’t like those chevrons)
Hey, if you’re like me, hhhmmmmm that xaml is nice, but uuuhhh PROVE IT! Ok!
Notice, how all the presenters profiles are lining up properly on the right?! COOL! THAT’S what I was looking for! Merci Laurent pour ton aide!
Now that I got THAT width thing figured out, it’s time to grab a coffee and get coding!
Laurent Duveau: http://weblogs.asp.net/lduveau/
PCHenry.com: I just released my fourth WP7 app tonight! YAHOO! DevTeach Ottawa 2011
Nilesh Gule’s Technical Blog: NGTweet Part3: Use Silverlight Themes to enhance UI (search for ‘stretch’ on thi spage).
stackoverflow: Silverlight 3: ListBox DataTemplate HorizontalAlignment
stackoverflow: How to get a ListBox ItemTemplate to stretch horizontally to full width of the ListBox?