簡體   English   中英

xaml 如何引用動態資源而不是作為屬性而是作為元素

[英]xaml How reference a dynamic resource not as a attribute but as an element

我有一張圖片需要在我的應用程序中的幾個地方使用。 我想在資源字典中只定義一次圖像,並讓我的其他 xaml 文件只是那個定義。 我能想到的一件事是如何引用定義為 xaml 元素而不是 xaml 屬性內部的屬性。

這是我的資源字典

# resourceDictionary.xaml

<LinearGradientBrush x:Key="MyGradient" StartPoint="0,0.5" EndPoint="1,0.5">
    <GradientStop Color="#A5000000" Offset="0"/>
    <GradientStop Color="#00000000" Offset="1"/>
</LinearGradientBrush>

<Image x:Key="MyImage" Source="MyGlyph.png" Width="20" Height="20" />

所以在我的 xaml 我知道如何引用漸變作為控件的屬性 object

<TextBlock Text="Sample Text" Background="{DynamicResource MessageGradient}"/>

但我想弄清楚他如何引用圖像,這是一個完整的控件 object。 此示例僅創建一個按鈕,按鈕中包含文本“{DynamicResource MyImage}”而不是圖像。

<!-- Want MyImage to be content of the button -->
<Button>
   {DynamicResource MyImage}
</Button>

有沒有一種簡單的方法可以做到這一點,或者我必須創建一個只包含我的圖像的控制模板,然后在我的 xaml 中有一個使用控制模板的圖像標簽?

由於您希望圖像成為按鈕的內容,因此您應該能夠將其綁定到Content屬性:

<Button Content="{DynamicResource MyImage}" />
<!-- Want MyImage to be content of the button --> 
<Button> 
   <DynamicResource  ResourceKey="MyImage"/>
</Button> 

如果您嘗試將圖像作為任何控件的背景

  <Button  Content="My Button">
        <Button.Background>
            <ImageBrush   ImageSource="MyGlyph.png"/>
        </Button.Background>
    </Button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM