为什么来自resourcedtion的XAML Canvas只能显示一次?

本文关键字:显示 一次 resourcedtion Canvas XAML 为什么 | 更新日期: 2023-09-27 18:08:24

在我的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正确的形式),它将显示所有列表项,但不是当我试图从资源中显示它时。

为什么来自resourcedtion的XAML Canvas只能显示一次?

我明白了。忘记添加共享属性

感谢@tym32167