在XAML中绑定图像源

noo*_*oob 8 silverlight xaml image

我试图通过c#将图像源绑定到我的XAML

这很有效

<Image Source="images/man.jpg"></Image>
Run Code Online (Sandbox Code Playgroud)

这不起作用

<Image Source="images/{Binding imagesource}"></Image>
Run Code Online (Sandbox Code Playgroud)

其中imagesource是此xaml的c#文件中的字符串变量,并且被设置为等于"man.jpg"

dev*_*xer 0

imagesource需要是一个实际的Image对象,而不是string.

Image这是一个在给定路径的情况下创建新对象的方法:

public BitmapImage Load(string path)
{
    var uri = new Uri(path);
    return new BitmapImage(uri);
}
Run Code Online (Sandbox Code Playgroud)