2016-08-03 120 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

相關問題