Tuesday, October 06, 2009

XAML Wrap Panel Tip

I recently need to display a set of items in a ListBox in a grid that wrapped based on the width of the ListBox. Simple right?

My XAML started out something like this:

<Grid x:Name="LayoutRoot">
<ListBox Margin="170,57,134,150">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
</ListBox>
</Grid>




and it produced the following result:



wrappanelproblem



Notice how the 5th button is cutoff and is not wrapping. For awhile I was stumped and started looking at ways to modify the WrapPanel itself.  One solution might be to give the WrapPanel a fixed width, but that is not very elegant. The solution I landed on (and what I believe to be the correct solution) is to disable the Horizontal Scrolling functionality on the ListBox.



The XAML changes to this:



<Grid x:Name="LayoutRoot">
<ListBox Margin="170,57,134,150" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
<Button Width="75" Content="Button"/>
</ListBox>
</Grid>




The attached ScrollViewer.HorizontalScrollbarVisibility property tells the Scrollviewer the ListBox (and thus its containing panel) not to scroll horizontally. Thus the WrapPanel doesn’t assume it has an infinite width (this is a non-developer interpretation of what is going on).



The result is just what I want:



WrapPanelIdeal



Hope this helps!

Monday, October 05, 2009

Dating advice

speed-dating-end My friend, a woman (let’s call her Jane), just asked me dating advice. Why I don’t know because I am probably not the best to give it, but her question went something like this (over text message):

Went on a date.  Was ok.  Not bad at all but not smitten. Kissed goodnight. Agreed to a 2nd date. Now having reservations.  If I am not super into him then why waste time.

I couldn’t agree more – and not just because of the time she would be wasting. Both parties would be wasting time. If there is no connection then move on. There are plenty of other people out there. 

However, most girls I know don’t throw kisses around on the first date unless they are into the other party. With that in mind I think Jane should think about the following:

- If she goes on many dates then she should abort any future dates with this guy. Even though they kissed, not a big deal, Jane does this a lot maybe (not judging here)

- If she doesn’t go on many dates and she kissed this guy, and that is rare for her, then she should give it another go. There must have been a connection. The guy is worth another shot. Maybe he was nervous, not acting like his normal self – there could be more there.

-  If Jane was drunk at all at the time then she should cut ties now. The kiss may have sounded good at the time, but not afterwards, and since Jane isn’t smitten then its not going to get better

But what do I know…

Tune in next time to find out what Jane does!

Zune Card