views:

1468

answers:

3

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?

+4  A: 

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}" />
Andy
A: 

If you are trying to put an Image as Background of any control

  <Button  Content="My Button">
        <Button.Background>
            <ImageBrush   ImageSource="MyGlyph.png"/>
        </Button.Background>
    </Button>
Jobi Joy
A: 
<!-- Want MyImage to be content of the button --> 
<Button> 
   <DynamicResource  ResourceKey="MyImage"/>
</Button> 
Akash Kava