问题
I have a Style setter that sets the value to a resource.
It works for a single element, but when the Style is applied to more than 1 element, an exception is thrown. This is what I have:
<SymbolIcon x:Key="Star" Symbol="Star" />
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource Star}"/>
</Style>
I understand that only one instance will be created. Since I cannot create "multi-instance" resources , how could I make it work?
回答1:
If you want to make couple elements, not only one instance, then you can use templates. A simple example:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<DataTemplate x:Key="Star">
<SymbolIcon Symbol="Favorite"/>
</DataTemplate>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Star}"/>
</Style>
</Grid.Resources>
<StackPanel Orientation="Horizontal">
<ContentControl/>
<ContentControl/>
<ContentControl/>
<ContentControl/>
</StackPanel>
</Grid>
来源:https://stackoverflow.com/questions/33388126/workaround-for-xshared-in-universal-applications