如何在WPF中使用动态URL绑定到图像? [英] How to bind to an Image using a dynamic URL in WPF?
问题描述
我是WPF的新手,所以希望我能正确说出这个问题。我想做的是将< Image>
绑定到在线图像。但是,我要绑定的图像根据应用程序的状态而变化。例如,如果我想绑定到从列表中选择的Employee,我将从App.config中检索基本URL并使用该雇员的ID附加图像名称,如下所示:
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);
问题是,我不确定如何在WPF中以声明方式进行操作。
The problem is, I'm not sure how to do this declaratively in WPF. Any help is greatly appreciated!
推荐答案
代码中是否有雇员对象?如果是这样,您可以公开基于该对象的员工ID构建的URI属性。
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.
否则,您的网站上是否会有一个asp.net页面可以提供服务图像(我不知道这样是否行得通,尽管这是一个想法)
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)
因此在您的xaml中有这样的内容
so have something like this in your xaml
<Image Source="{Binding Path=EmployeeId, StringFormat='http://my.url.com/Image.aspx?employeeId={0}'}" />
Image.aspx是否会根据employeeId get变量流式传输图像?
Image.aspx would stream the image based on the employeeId get variable?
正如我说的那样,这可能有点错误,但是它可以工作,我认为雇员类的URI属性将是最干净的选择。
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.
这篇关于如何在WPF中使用动态URL绑定到图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!