为什么ResourceDiction中的XAML Canvas只能显示一次?

时间:2016-09-23 12:17:51

标签: c# wpf xaml

我的WPF应用程序中有一个ResourceDictionary,其中包含一堆Canvas个图标。我想多次在其中一个表单上显示这些图标。但是,出于某种原因,如果从ResourceDictionary加载Canvas它只会显示一次,那么列表元素将呈现最后一个。那是为什么?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas x:Key="ValueBox" Width="473.8" Height="473.8">
        <Canvas>
            <Path Fill="#000000">
                <Path.Data>
                    <PathGeometry Figures="M454.8 111.7c0-1.8-0.4-3.6-1.2-5.3c-1.6-3.4-4.7-5.7-8.1-6.4L241.8 1.2c-3.3-1.6-7.2-1.6-10.5 0L25.6 100.9   c-4 1.9-6.6 5.9-6.8 10.4v0.1c0 0.1 0 0.2 0 0.4V362c0 4.6 2.6 8.8 6.8 10.8l205.7 99.7c0.1 0 0.1 0 0.2 0.1   c0.3 0.1 0.6 0.2 0.9 0.4c0.1 0 0.2 0.1 0.4 0.1c0.3 0.1 0.6 0.2 0.9 0.3c0.1 0 0.2 0.1 0.3 0.1c0.3 0.1 0.7 0.1 1 0.2   c0.1 0 0.2 0 0.3 0c0.4 0 0.9 0.1 1.3 0.1c0.4 0 0.9 0 1.3-0.1c0.1 0 0.2 0 0.3 0c0.3 0 0.7-0.1 1-0.2c0.1 0 0.2-0.1 0.3-0.1   c0.3-0.1 0.6-0.2 0.9-0.3c0.1 0 0.2-0.1 0.4-0.1c0.3-0.1 0.6-0.2 0.9-0.4c0.1 0 0.1 0 0.2-0.1l206.3-100c4.1-2 6.8-6.2 6.8-10.8   V112C454.8 111.9 454.8 111.8 454.8 111.7z M236.5 25.3l178.4 86.5l-65.7 31.9L170.8 57.2L236.5 25.3z M236.5 198.3L58.1 111.8   l85.2-41.3L321.7 157L236.5 198.3z M42.8 131.1l181.7 88.1v223.3L42.8 354.4V131.1z M248.5 442.5V219.2l85.3-41.4v58.4   c0 6.6 5.4 12 12 12s12-5.4 12-12v-70.1l73-35.4V354L248.5 442.5z" FillRule="NonZero"/>
                </Path.Data>
            </Path>
        </Canvas>
    </Canvas>
</ResourceDictionary>

我正在尝试将其作为列表中特定项目的图标显示在列表中。

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Images/VectorIcons.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

...

<DataTemplate DataType="{x:Type model:SomeContainerClass}">
    <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
        <Viewbox Width="16" Height="16">
            <ContentControl Content="{StaticResource ValueBox}"/>
        </Viewbox>
    </StackPanel>
</DataTemplate>

同时如果我将相同的画布直接插入窗口的xaml(在窗体中的Viewbox右侧),它将显示所有列表项但不会在我尝试显示它时资源。

1 个答案:

答案 0 :(得分:0)

想出来。忘记添加Shared attribute

感谢@ tym32167!