Silverlight UI Rant #2 – ListBoxItem

0 comments

Posted on 12th February 2009 by r2 in Silverlight |UI Rant

Tonight’s recipient of my UI Rant is the Silverlight 2 ListBox, or more specifically, the ListBoxItem.  A client recently asked me to provide an alternating row style like the DataGrid for the ListBox.  Now, if you’ve ever tried to add a border or background to your ListBoxItem, you’ve seen this:

DefaultlListBox

No problem, you say, I will simply set the HorizontalContentAlignment of my ListBox to Stretch and all should be good.  When that doesn’t do it, you’ll probably try creating/editing the ListBox’s ItemTemplate….maybe adding a Grid and setting its HorizontalAlignment to Stretch.  This too will fail.

Turns out, the problem is not with your ItemTemplate or even with the ListBox itself.  The issue is in the container that actually contains your Item and that is the ListBoxItem.  The property on the ListBox that allows you to set a custom style for this ListBoxItem is called ItemContainerStyle.  Armed with this information, you quickly throw together a style for your ListBoxItem, maybe like so:

  1. <Style TargetType="ListBoxItem" x:Key="ItemContainerStyle">
  2.     <Setter Property="Padding" Value="3" />
  3.     <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  4.     <Setter Property="VerticalContentAlignment" Value="Top" />
  5.     <Setter Property="Background" Value="Transparent" />
  6.     <Setter Property="BorderThickness" Value="1" />
  7. </Style>

And then add that to your ListBox, like so:

  1. <ListBox Margin="8,8,10,8" x:Name="defaultListBox" ItemsSource="{Binding}"
  2.     ItemTemplate="{StaticResource EmployeeItemTemplate}"  
  3.          ItemContainerStyle="{StaticResource ItemContainerStyle}"/>

Hit F5 and just about the time you congratulate yourself on solving this, the Silverlight Loading Animation will finish and you’ll discover that your ListBox still looks exactly like the original screenshot above.  So, what happened?

What happened is that the default ControlTemplate for the ListBoxItem has its HorizontalAlignment hard-coded to Left and no matter what you do when setting the properties, it will always be Left.  If you’ll do a little diving into the full default style for ListBoxItem, courtesy of SilverlightDefaultStyleBrowser (highly recommended), you’ll see exactly what I’m talking about in the ContentPresenter’s HorizontalAlignment property:

ListBoxItemDefaultStyle

The good news is that because you have the ability to replace the Template of any control in Silverlight, you can simply "correct" this hard-coded problem by replacing the hard-coded Left value for HorizontalAlignment with {TemplateBinding HorizontalContentAlignment}, so your final new Style would look like this:

  1. <Style TargetType="ListBoxItem" x:Key="StretchedItemContainerStyle">
  2.     <Setter Property="Padding" Value="3" />
  3.     <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  4.     <Setter Property="VerticalContentAlignment" Value="Top" />
  5.     <Setter Property="Background" Value="Transparent" />
  6.     <Setter Property="BorderThickness" Value="1" />
  7.     <Setter Property="TabNavigation" Value="Local" />
  8.     <Setter Property="Template">
  9.         <Setter.Value>
  10.             <ControlTemplate TargetType="ListBoxItem">
  11.                 <Grid Background="{TemplateBinding Background}">
  12.                     <VisualStateManager.VisualStateGroups>
  13.                         <!– removed VSM code for brevity  –>
  14.                     </VisualStateManager.VisualStateGroups>
  15.                     <Rectangle x:Name="fillColor" Opacity="0" Fill="#FFBADDE9"   
  16.                        IsHitTestVisible="False" RadiusX="1" RadiusY="1" />
  17.                     <Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9"
  18.                        IsHitTestVisible="False" RadiusX="1" RadiusY="1" />
  19.                     <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}"
  20.                        ContentTemplate="{TemplateBinding ContentTemplate}"
  21.                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
  22.                        Margin="{TemplateBinding Padding}" />
  23.                     <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1"
  24.                          StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" />
  25.                 </Grid>
  26.             </ControlTemplate>
  27.         </Setter.Value>
  28.     </Setter>
  29. </Style>

Now your ListBoxItem will respect the value you are setting for its HorizontalContentAlignment in your Setter above and your ListBox will finally look like this:

CorrectedListBox

Hope that saves someone a little frustration.  I’ll continue on to adding the alternating row style in another post.

Here is the live sample.

Here is the source code.

No comments yet.

Sorry, the comment form is closed at this time.