Search code examples
wpfxamlstaticresourcesreusability

Why can a Path resource not be used by multiple StaticResource references?


Please!Who can help me to solve this problem: I have defined a path icon which is created by Metro Studio. just like this:

<Window.Resources>
    <!--ICO Resources-->
    <Path x:Key="CheckBoxOKICO" Data="M23.699997,8.8999939L26.099991,11.699997 13.099991,23.099991 5.8000031,14.599991 8.5999908,12.199997 13.5,17.899994z M1.6999969,1.6999969L1.6999969,30.300003 30.300003,30.300003 30.300003,1.6999969z M0,0L32,0 32,32 0,32z" Stretch="Uniform" Fill="#FF161616" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
        <Path.RenderTransform>
            <TransformGroup>
                <TransformGroup.Children>
                    <RotateTransform Angle="0" />
                    <ScaleTransform ScaleX="1" ScaleY="1" />
                </TransformGroup.Children>
            </TransformGroup>
        </Path.RenderTransform>
    </Path>

It was a vector icon:enter image description here I want to reuse it in my WPF window UI:

       <!--Button1 is OK-->
    <Button x:Name="btnTaskItem1" Grid.Row="0">
        <Button.Content>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"  Width="220">
                <Viewbox Width="18" Height="18" Child="{StaticResource CheckBoxOKICO}"/>
                <TextBlock x:Name="txtTaskItem1" Margin="5,0"  Text="binding Task Item1"/>
            </StackPanel>
        </Button.Content>
    </Button>
    <!--Button2 is No OK-->
    <Button x:Name="btnTaskItem2" Grid.Row="1">
        <Button.Content>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"  Width="220">
                <Viewbox Width="18" Height="18" Child="{StaticResource CheckBoxOKICO}"/>
                <TextBlock x:Name="txtTaskItem2" Margin="5,0"  Text="binding Task Item2"/>
            </StackPanel>
        </Button.Content>
    </Button>

What I want is just like this: enter image description here

But When I run the simple test program,it gave me a error message:

Other information: "set the property" System.Windows.Controls.Viewbox.Child "when the exception is thrown." That number is "81", "41 line position".

If I delete the second staticResource where used in the second button , it works well! Why I can not reuse the staticResource?! Thanks for your any tips!


Solution

  • Visual elements can only ever have a single parent. By referencing it twice you add it to multiple parents. Set x:Shared="false" on the resource to make it clone.