Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF XAML syntax for including resource as xml element rather than as property

Tags:

syntax

wpf

xaml

I've got this working:

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

But now I want to make the button a little more complicated

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

How do I place the image resource in the xml tree, rather than just assigning it to a property to the Button class?

Note, the resource is defined like:

<Image x:Key="SaveImage" x:Shared="False" Source="Save.png" Height="16" Width="16"/>
like image 310
Clyde Avatar asked Feb 25 '11 13:02

Clyde


People also ask

What is resource in XAML?

A resource is an object that can be reused in different places in your app. Examples of resources include brushes and styles. This overview describes how to use resources in Extensible Application Markup Language (XAML). You can also create and access resources by using code.

What is static resource in XAML?

A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.

What are dynamic resources in WPF?

Dynamic ResourceThe concept is the same as data binding in WPF if the value of the bound property is changed. So is the value on UI. Change XAML as follows: added a new button. Also, add 2 resources in the code behind.


1 Answers

You can use a StaticResource directly. Try it like this

<Button>
    <Button.Content>
        <StackPanel Orientation="Horizontal" >
            <StaticResource ResourceKey="SaveImage"/>
            <Label>Save</Label>
        </StackPanel>
    </Button.Content>
</Button>
like image 126
Fredrik Hedblad Avatar answered Nov 09 '22 11:11

Fredrik Hedblad