?
Solved

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

Posted on 2014-07-31
5
Medium Priority
?
1,528 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

752 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