Because everything inside a Silverlight resource dictionary must be shareable. In WPF, you can use the x:Shared attribute on objects inside a resource dictionary to force WPF to create a new instance for every resource retrieval.
To avoid this in Silverlight you can create a DataTemplate
:
<DataTemplate x:Key="ButtonTemplate">
<Viewbox>
<!-- Here your content-->
</Viewbox>
</DataTemplate>
<Button ContentTemplate="{StaticResource ButtonTemplate}"/>
UPDATE 0
I wrote an example that changes the template that depends on the value of the CheckBox
.
Converter to change a template:
public class TemplateSelectorConverter : IValueConverter
{
public DataTemplate TrueTemplate { get; set; }
public DataTemplate FalseTemplate { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((bool) value))
return TrueTemplate;
return FalseTemplate;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Resources:
<DataTemplate x:Key="FirstTemplate">
<TextBox Text="FirstTemplate" />
</DataTemplate>
<DataTemplate x:Key="SecondTemplate">
<TextBox Text="SecondTemplate" />
</DataTemplate>
<internal:TemplateSelectorConverter x:Key="TemplateSelector" TrueTemplate="{StaticResource FirstTemplate}"
FalseTemplate="{StaticResource SecondTemplate}" />
Xaml markup:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox Name="CheckBox"/>
<Button Grid.Column="1"
ContentTemplate="{Binding Path=IsChecked, ElementName=CheckBox, Converter={StaticResource TemplateSelector}}"
VerticalAlignment="Top" />
</Grid>
I hope this helps.