views:

2430

answers:

2

I am new to WPF so hopefully I phrased the question correctly. What I'd like to do is bind my <Image> to an image online. However, the image I would like to bind to changes depending on the state of the application. For example, if I wanted to bind to an Employee selected from a list, I'd retrieve a base URL from my App.config and append the image name using the ID of the employee, like so:

var baseUrl = ConfigurationSettings.AppSettings["BaseImageUrl"];
var imageUrl = String.Format("{0}/{1}.jpg", baseUrl, employeeID);

The problem is, I'm not sure how to do this declaratively in WPF. Any help is greatly appreciated!

A: 

I think something like this will help:

<Window.Resources>
   <ImageSource x:Key="MyImage" Source="C:\Images\Default.jpg" />
</..>

<Image Source="{DynamicResource MyImage}" />

Then in your code-behind:

((ImageSource)this.Resources["MyImage"]).Source = "C:\Path\From\Config.jpg";
Paul Stovell
Can that filesystem path be substituted for a URL? I think that's what the original question required (and I'm curious).
Drew Noakes
As far as I know, yes.
Paul Stovell
Thanks for the response. Great suggestion. However, I was hoping there might be a way to do this declaratively (without having to go into codebehind). Am I hoping for too much? :)
Kevin Babcock
Also, I would stay away from dynamic resources.
daub815
+2  A: 

Do you have a employee object in your code? If so you could expose a URI property which is built based on the employee ID of the object.

Otherwise could you have a asp.net page on your website which serves up a image (I have no idea if this will work, it is a idea though)

so have something like this in your xaml

<Image Source="{Binding Path=EmployeeId, StringFormat='http://my.url.com/Image.aspx?employeeId={0}'}" />

Image.aspx would stream the image based on the employeeId get variable?

As I said there is probably a bit wrong with this but it could work, I think the URI property on a employee class would be the cleanest option though.

Jake Ginnivan
This worked just great. Thanks for the help!
Kevin Babcock