Click here to Skip to main content
15,887,683 members
Articles / Desktop Programming / WPF
Tip/Trick

Accessing image from Resource File in XAML markup

Rate me:
Please Sign up or sign in to vote.
3.86/5 (7 votes)
9 Mar 2011CPOL 77.1K   3   3
Accessing image from Resource File in XAML markup
Suppose we have an image in our Resource file named as LeftBaseTriangle.bmp.

In order to access it via XAML, all we need is to add a reference to project properties like below:
XML
xmlns:properties="clr-namespace:Azad.Modules.MyProject.Properties"


and add the following code:

XML
<image source="{Binding Source={x:Static properties:Resources.LeftBaseTriangle}}" />

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Vizrt Bangladesh
Bangladesh Bangladesh
I am truly versatile and 360 degree Engineer having wide range of development experience in .NET and Java Platform. I am also proficient in system level programming in C++. To me technology is not at all a problem, it’s the client requirement that matters! That is I am ready and comfortable to use any technology to make the business of my client a success.

In my five years of experience I have the opportunities to work for fortune 500 companies of US and many renowned clients from Europe.

My Linkedin Profile: http://bd.linkedin.com/in/mahmudazad

Comments and Discussions

 
GeneralMy vote of 1 Pin
Karel Kral27-Jun-18 21:54
Karel Kral27-Jun-18 21:54 
QuestionNot working Pin
mikefulton8-Apr-15 12:32
mikefulton8-Apr-15 12:32 
It's four years since original post so I hope someone's listening...

This is not working for me.

The first problem is that I'm not sure I've setup the "properties"xmlns item correctly. I have it set to "MyProj.Properties " (where "MyProj" is the namespace for everything in the project).

Maybe I'm missing the equivalent of "Azad.Modules" from the example, but I dunno what my equivalent would be.

VS2013 isn't flagging anything in the XAML as an error at design time, but at runtime I get this:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.Markup.StaticExtension' threw an exception.' Line number '29' and line position '59'.


Here's my XAML. I removed some of the default xmlns items that VS2013 puts into the first tag, to simplify things.
<UserControl x:Class="TriviaGame.MediaElementsBrowserPanel"
             xmlns:properties="clr-namespace:TriviaGame.Properties"
             mc:Ignorable="d" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             d:DesignHeight="294" d:DesignWidth="738" Loaded="UserControl_Loaded">
    <Grid>
        <WrapPanel Background="#FFFFB8B8" Margin="0,0,0,0">
            <Button Width="24" Height="24" Click="Button_Click" Padding="0" Margin="5,0,0,0" Background="{x:Null}">
                <Image Height="24" Width="24" Source="{Binding Source={x:Static properties:Resources.icon_delete}}"/>
            </Button>
        </WrapPanel>
    </Grid>
</UserControl>

AnswerRe: Not working Pin
Ollikat7-Jul-16 0:35
Ollikat7-Jul-16 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.