views:

28

answers:

1

I am trying to design a set of icons in a Silverlight 4 User Control's ressources, and then display these on a button.

I have

<UserControl.Resources>        
    <Rectangle x:Key="Icon1" Fill="Black" Width="10" Height="10" />            
</UserControl.Resources>

and

<Button x:Name="Button1"
        Width="50" Height="50"                        
        Content="{Binding Source={StaticResource Icon1}}" /> 

I also tried ... Content="{StaticResource Icon1}". Both show fine in the XAML Designer of VS 2010, but fail when I try to run it with a XAMLParseException. The first one complains about the argument not falling into the expected range, the second one simply says "Failed to assign property". Copying the Rectangle into the Buttons content directly works fine.

Where is the problem? I thought I finally understood this.. =/

+1  A: 

I would suggest using a template instead of setting the content anyway.

<ControlTemplate
    x:Key="IconTemplate">
       <Rectangle x:Key="Icon1" Fill="Black" Width="10" Height="10" />   
</ControlTemplate>

<Style x:Key="IconStyle" TargetType="Button">
       <Setter Property="Template" Value="{StaticResource IconTemplate}"/>
</Style>

<Button x:Name="Button1"
        Width="50" Height="50"                        
        Style="{StaticResource IconStyle}" /> 

HTH

Chris Nicol
Thanks for your answer! I'd like to know, why you would choose this approach instead of setting the content directly?
Jens
Chris Nicol