10

Xaml How to reference a dynamic resource not as an attribute but as an element

 2 years ago
source link: https://www.codesd.com/item/xaml-how-to-reference-a-dynamic-resource-not-as-an-attribute-but-as-an-element.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xaml How to reference a dynamic resource not as an attribute but as an element

advertisements

I have an image I need to use in my application in several places. I want to define the image just once in a resource dictionary, and have my other xaml files just that definition. I can The one thing I haven't bee able to figure out is how to reference something defined as a xaml element instead of a attributed inside of a xaml attribute.

Here is my ResourceDictionary

# 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" />

So in my xaml I know how to reference the gradient as an attribute of a control object

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

But what I want to figure out his how to reference the Image which is a full blown control object. This example just creates a button that has the text "{DynamicResource MyImage}" in the button instead of the image.

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

Is there a simple way to do this, or will have I have to create a control template that contains just my image, and then in my xaml have a image tag that uses the control template?


Since you want the image to be the content of the button, you should be able to bind that to the Content property:

<Button Content="{DynamicResource MyImage}" />


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK