本文介绍了创建staticsource的多个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于项目控制的模板,如下所示.我需要模板中每个项目的colorProvider单独实例.项目控件中的每个项目都需要单独的Color Provider实例,具体取决于绑定到的项目.如何创建staticresource的多个副本,以便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>

推荐答案

要在每次请求时返回静态资源的新实例,可以使用x:Shared属性. MSDN .根据我对该属性的经验,尝试设置该属性时,您会获得Intellisense支持.在您的情况下,需要在资​​源"部分的ColorProvider上设置属性,如下所示.

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>

这篇关于创建staticsource的多个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!