Solved

How to bind a local image file to an Image control in xaml?

Posted on 2014-07-31
5
1,429 Views
Last Modified: 2014-08-07
First, thank you for your time.

My question is ultimately how to bind a local png file to an Image control on the xaml using the mvvm pattern and binding.  I am confused about whether to use:
1) <Image Source={Binding MyImage} .../>
2) <Image Source={Binding Path=MyImage} .../>
3) <Image>
            <Image.Source>
                  <BitmapImage UriSource="{Binding MyImageUri}" />
            </Image.Source>
     </Image>
4) Should I use my own converter or use Microsoft's ImageSourceConverter.

Will you please enlighten me?  Please show me the appropriate xaml code along with the corresponding c# code that will load the local png file from disk and make it appear in my view.

Thank you again for your time.
0
Comment
Question by:MichaelDavidCarr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40234835
When you say "local", it sounds like you might want to get an image on the actual machine running the Silverlight, and you do not get access to the machine's file system.

If you mean local to your project, it depends on whether you have the image in there as content, embedded, etc.
0
 

Author Comment

by:MichaelDavidCarr
ID: 40235264
Sorry, I didn't give you enough details about the project.  I am creating a Windows desktop application not an in-browser application.  So, I do believe I have access to the file system.

Again, thank you for your time.
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 40235328
Sorry, I should have figured this out from it being WPF rather than Silverlight!   Almost all of my XAML has been Silverlight rather than WPF.

With WPF you can just do

            image.Source = new BitmapImage(new Uri(@"file:///c:/users/jens/pictures/parkour.jpg"));
0
 

Author Comment

by:MichaelDavidCarr
ID: 40235505
Thank you for responding yet again.

I followed your instructions and my image is still not showing.  Here is the code:

xaml:
       <ComboBox ItemsSource="{Binding Bills}" SelectedValue="{Binding BillSelected}" 
       <Image  Source="{Binding BillImageSource" />

c#:
        public ImageSource BillImageSource;

        // When a bill is selected from the combobox, this property is invoked with the bill that was selected. 
        public string BillSelected
        {
            set
            {
                var selectedBillName = value;
                var filename = _repository.GetBillFilenameByShortName(selectedBillName);
                var uri = new Uri(@"file:" + filename);
                var bitmap = new BitmapImage(uri);
                BillImageSource = bitmap;
                OnPropertyChanged("BillImageSource");
	    }
	}

Open in new window


As I step through the code, everything looks okay to me, and yet the png file that corresponds to the filename doesn't show.
What am I doing wrong?

Thank you for your time Jens.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40235508
Take a look at "filename"....does it look like this, like in my code?

///c:/users/jens/pictures/parkour.jpg

I'd be kind of surprised if it did, I think you might need to do some string munging to get this right.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question