Search code examples
c#wpfxamlcontentpresenter

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


I am Trying to Display a bunch of Names with an Icon in a particular Scroll viewer. There are 2 main types of Icons that need to be displayed , However although both Types pf Icons are disgustingly being displayed only One Element gets this Icon at a time.


Solution

  • Here is a minimal example that verifies, that a resource with x:Shared="False" can be used to render multiple occurences of the same icon, while with a shared resource (default), only the last occurence is rendered:

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800"
            x:Class="Application1.MainWindow">
        <Window.Resources>
            <Canvas x:Key="FileIconShared" Width="20" Height="20">
                <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">
                </Path>
            </Canvas>
            <Canvas x:Key="FileIconNotShared" x:Shared="False" Width="20" Height="20">
                <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">
                </Path>
            </Canvas>
        </Window.Resources>
        <Grid>
            <StackPanel HorizontalAlignment="Left" Width="100">
                <TextBlock>Shared 1</TextBlock>
                <ContentControl Content="{StaticResource FileIconShared}"/>
                <TextBlock>Shared 2</TextBlock>
                <ContentControl Content="{StaticResource FileIconShared}"/>
                <TextBlock>Not Shared 1</TextBlock>
                <ContentControl Content="{StaticResource FileIconNotShared}"/>
                <TextBlock>Not Shared 2</TextBlock>
                <ContentControl Content="{StaticResource FileIconNotShared}"/>
            </StackPanel>
        </Grid>
    </Window>
    

    Effect: First Icon is missing, 2nd to 4th is rendered.

    I can't say how you would apply this to your project, since to much information is missing on where exactly your resources are defined and how exactly your datatemplate is configured.