2016-08-03 60 views
1

请谁帮我解决这个问题: 我已经定义了一个由Metro Studio创建的路径图标。就像这样:为什么Path资源不能被多个StaticResource引用使用?

<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> 

这是一个矢量图标:enter image description here 我想重新使用它在我的WPF窗口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> 

我要的就是这样的: enter image description here

但是当我运行简单的测试程序时,它给了我一个错误信息:

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

如果我删除第二个按钮中使用的第二个staticResource,它会很好用! 为什么我不能重用staticResource?! 感谢您的任何提示!

回答

2

视觉元素只能有一个父亲。通过引用它两次,将它添加到多个父母。在资源上设置x:Shared="false"以使其克隆。

+0

非常感谢你!它现在有效〜 – Wuying283

1

一个元素一次只能出现在一个可视化树中。您的路径现在出现在两个按钮的可视化树中。

要么使用x:Shared="false"要么只是使用创建一个更多的资源,只改变其Key

用此PathViewBoxPath创建UserControl

+0

好人!愿你每天都快乐。 非常感谢,您的回答是正确的,但只有一个正确的答案可以在此网页中选择。 – Wuying283

+0

@ Wuying283如果你这么想,你可以改变正确的答案。点击刻度标记以取消标记。 – AnjumSKhan

相关问题