Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create multiple copies of staticresource

I have a template for items control shown below. I need separate instances of colorProvider for each item in the template. Each item in the items control requires a seperate instance of the Color Provider depending on the item it is bound to. How do i create multiple copies of staticresource so that the staticresource is only available for that item.

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding DataList}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid MinHeight="250">
                        <ContentPresenter Content="{Binding }" ContentTemplateSelector="{StaticResource chartSelector}">
                            <ContentPresenter.Resources>
                                <v:ColorProvider x:Key="colorProvider"/>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
like image 576
TrustyCoder Avatar asked Feb 19 '16 17:02

TrustyCoder


1 Answers

To return a new instance of a static resource each time it's requested, you can use the x:Shared attribute. This is documented on MSDN. From my experience with this attribute, you will not get Intellisense support when trying to set it. In your case, the attribute would need to be set on the ColorProvider in your Resources section, as follows.

<ContentPresenter Content="{Binding }" ContentTemplateSelector="{StaticResource chartSelector}">
  <ContentPresenter.Resources>
    <v:ColorProvider x:Key="colorProvider" x:Shared=false />
  </ContentPresenter.Resources>
</ContentPresenter>
like image 119
Michael Avatar answered Oct 17 '22 19:10

Michael