R – ItemContainerStyle in Custom Control Derived From ListBox

custom-controlslistboxsilverlightsilverlight-3.0xaml

Please bear with me Silverlight Designer Gurus, this is compicated (to me).

I'm creating a custom control which derives form the Silverlight 3.0 ListBox. In an effort not to show tons of code (initially), let me describe the setup.

I have a class library containing a class for my control logic. Then I have a Themes/generic.xaml that holds the styling details. In generic.xaml, I have a style that defines the default layout and look for the ListBox where I'm setting a values for the Template, ItemsPanel and ItemTemplate.

In my test app, I add my control on to MainPage.xaml and run it and it works great. I dynamically bind data to my control and that works fine.

Now I want to set the ItemContainerStyle for my derived control. If I create a style in the MainPage.xaml file and set the ItemContainerStyle property to that control as in:

<dti:myControl x:Name="MyControl1" ItemContainerStyle="{StaticResource MyListBoxItem}"
                                  Height="500" 
                                  Width="200" 
                                  Margin="10"
                                  Background="AliceBlue"
                                  />

It works as expected.

However, I'd like to do this in the class library or, more specifically, in generic.xaml. I tried to this Setter to my current Style:

<Setter Property="ItemContainerStyle">
  <Setter.Value>
    <ControlTemplate>
      <Grid Background="Red" Margin="3">
        <ContentPresenter x:Name="contentPresenter"
          ContentTemplate="{TemplateBinding ContentTemplate}"
            HorizontalAlignment="Stretch" Margin="3"/>
      </Grid>
    </ControlTemplate>
  </Setter.Value>
</Setter>

And it fails miserably with:

"System.ArgumentException: 'System.Windows.Controls.ControlTemplate' is not a valid value for property 'ItemContainerStyle'."

Note: This is not my actual style I'd like to use for ItemContainerStyle. I'm actually looking to plug in some VSM here for the various selected/unselected states of the a ListBoxItem (for a dynamically bound control).

So, to the question is how do I apply the ItemContainterStyle to my custom control when it's defined using generic.xaml? I do not want that property set when I actually use the control later on.

Thanks,

Beaudetious

Best Answer

You missed to put Style tag inside your Setter.Value. ItemContainerstyle explects a Style to ListBoxItem(Unless you subclassed ListBoxItem to your own derived version.)

<Setter Property="ItemContainerStyle"> 
   <Setter.Value>  
    <Style TargetType=”{x:Type ListBoxItem}“ >
      <Setter Property="Template">
        <Setter.Value>            
            <ControlTemplate> 
          <Grid Background="Red" Margin="3">        
              <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="Stretch" Margin="3"/> 
         </Grid> 
     </ControlTemplate>
    <Setter.Value>
    </Style>
 </Setter.Value>