Search code examples
c#wpfxamlcontentpresenter

Mandatory Use of Setting x:Shared="False" Not Possible


This Question might be Very Similar to Questions such as Error when using x:Shared="False" resources in external assembly in WPF but I have not been able to find a Solution that I can Relate to or rather get an Idea of How to Solve this.

Why I have mentioned Mandatory Use in the title is I have not been able to find an alternative way to solve my problem other than the with the use of Setting X:shared to false.

My problem is that in a particular view that has Icons , for Elements of Similar Type Icons seem to be sharing hence even if there are two or more types the Icon will only be rendered with one Item

This Question is justified in these Stack oveerflow Questions as well

Content Only being shown in a Single element at a given time

WPF: Can use StaticResource only once

I would Really appreciate any help/suggestions to overcome this


Solution

  • I might look at using DataTemplates to create the icons, if for any reason x:Shared is a problem. A DataTemplate instantiates a copy of the content when it's applied, so sharing is a non-issue.

    Resource:

    <DataTemplate x:Key="FileSystemIcon">
        <Canvas Width="12" Height="12">
            <Path 
                Stroke="Black" 
                Fill="White"  
                Data="M20,4L4,4A2,2,0,0,0,2,6L2,18A2,2,0,0,0,4,20L20,20A2,2,0,0,0,22,18L22,6A2,2,0,0,0,20,4 M20,18L4,18 4,8 12,13 20,8 20,18 M20,6L12,11 4,6 4,6 20,6 20,6z" 
                />
        </Canvas>
    </DataTemplate>
    

    Usage:

    <UserControl ContentTemplate="{StaticResource FileSystemIcon}" />