5

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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";

5 Comments

Can that filesystem path be substituted for a URL? I think that's what the original question required (and I'm curious).
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? :)
Also, I would stay away from dynamic resources.
@daub815 why would you stay away from dynamic resources?
It uses up more resources, so you should probably only use it when you absolutely need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.