2011-02-25 27 views
6

我得到這個工作:WPF XAML語法包括資源爲XML元素,而不是財產

<Button Content="{StaticResource SaveImage}" /> 

但現在我想使按鈕稍微複雜一點

 <Button> 
      <Button.Content> 
       <StackPanel Orientation="Horizontal" > 
        {StaticResource SaveImage} <!-- WHAT GOES HERE?? --> 
        <Label>Save</Label> 
       </StackPanel> 
      </Button.Content> 
     </Button> 

如何將圖像資源放置在xml樹中,而不是將其分配給Button類的屬性?

注意,資源的定義如下:

<Image x:Key="SaveImage" x:Shared="False" Source="Save.png" Height="16" Width="16"/> 

回答

17

您可以直接使用StaticResource。嘗試喜歡這個

<Button> 
    <Button.Content> 
     <StackPanel Orientation="Horizontal" > 
      <StaticResource ResourceKey="SaveImage"/> 
      <Label>Save</Label> 
     </StackPanel> 
    </Button.Content> 
</Button> 
+0

這似乎有竅門!雖然對我來說,它是而不是 - 也許這是一個3.5 - > 4.0的差異? – Clyde 2011-02-25 14:59:40

+0

@Clyde:3.5和4.0都應該是' 2011-02-25 15:28:48

+0

好吧,只是 Clyde 2011-02-25 16:04:57

0
<Image Source="{StaticResource SaveImage}"/> 
+0

圖像的源屬性可以,本身是另一個形象最簡單的方法?參見上面我已經添加了我的資源的定義。 – Clyde 2011-02-25 14:06:57

+0

是的,這是給我一個編譯錯誤 – Clyde 2011-02-25 14:09:57

+0

你的資源需要是一個BitmapSource,例如BitmapImage – alimbada 2011-02-25 14:21:12

0

在大多數這些情況去只是使用內容控件內

<Button> 
    <ContentControl Content="{StaticResource whatever}" /> 
</Button>