如何使用WPF中的动态URL绑定到图像?

时间:2022-09-03 21:21:58

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:

我是WPF的新手,所以希望我正确地表达了这个问题。我想做的是将我的如何使用WPF中的动态URL绑定到图像?绑定到在线图像上。但是,我想要绑定的图像会根据应用程序的状态而发生变化。例如,如果我想绑定到从列表中选择的Employee,我将从我的App.config中检索一个基本URL,并使用该员工的ID附加图像名称,如下所示:

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!

问题是,我不确定如何在WPF中以声明方式执行此操作。任何帮助是极大的赞赏!

2 个解决方案

#1


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.

您的代码中是否有员工对象?如果是这样,您可以公开基于对象的员工ID构建的URI属性。

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)

否则你可以在你的网站上有一个asp.net页面提供图像(我不知道这是否有效,但这是一个想法)

so have something like this in your xaml

所以在你的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?

Image.aspx会根据employeeId get变量传输图像吗?

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.

正如我所说,这可能有点不对,但它可以工作,我认为员工类的URI属性将是最干净的选择。

#2


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

#1


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.

您的代码中是否有员工对象?如果是这样,您可以公开基于对象的员工ID构建的URI属性。

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)

否则你可以在你的网站上有一个asp.net页面提供图像(我不知道这是否有效,但这是一个想法)

so have something like this in your xaml

所以在你的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?

Image.aspx会根据employeeId get变量传输图像吗?

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.

正如我所说,这可能有点不对,但它可以工作,我认为员工类的URI属性将是最干净的选择。

#2


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