Xaml As a link to a dynamic resource, not as an attribute, but as an element

I have an image that needs to be used in my application in several places. I want to define an image only once in the resource dictionary and have my other xaml files just for that definition. I can. The only thing I can’t understand is how to refer to something defined as an xaml element, instead of the attribute inside the 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 refer to a gradient as an attribute of a control object

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

But I want to figure out how to reference an image, which is a full-blown control object. This example simply 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>

, , , xaml , ?

+3
3

, , Content:

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

If you are trying to put an image as the background of any control

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

Source: https://habr.com/ru/post/1704461/


All Articles