2015-11-17 38 views
0

我在Application.Resources(SVG圖標)資源:如何從靜態資源創建一個圖標兩次?

<Canvas x:Key="IconAdd" Width="48" Height="48"> 
... 

我試圖用它在多個按鈕那樣:

<Button> 
    <StackPanel> 
     <Viewbox> 
      <ContentPresenter Content="{StaticResource IconAdd}"/> 
     </Viewbox> 
     <TextBlock>Button text</TextBlock> 
    </StackPanel> 
</Button> 

的問題是,該圖標只出現在上一個按鈕。

從這個answer我明白這是因爲在WPF中每個Visual只能有一個父項。

我覺得我需要把一個圖標放到數據模板中,但是如何在我的情況下使用它?

+0

發生這種情況是因爲Canvas是靜態的(對於所有應用程序都是一個實例),您可以嘗試使用DataTemplate,但我不確定如何,但這應該是方式 –

回答

0

正如我所料,我需要把一個圖標,數據模板:

<DataTemplate x:Key="IconAddTemplate"> 
    <Canvas Width="48" Height="48"> 
... 

然後,我可以用它這樣的:

<Button> 
    <StackPanel> 
     <Viewbox> 
      <ContentPresenter ContentTemplate="{StaticResource IconAddTemplate}" /> 
     </Viewbox> 
     <TextBlock>Button text</TextBlock> 
    </StackPanel> 
</Button>