Solved

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

Posted on 2014-07-31
5
1,459 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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