这里的绑定是没有问题的,问题在于绑定发生在你设置DataContext之前,所以Image路径还没有设置成你期望的值。
一种解决方法是在xaml中去掉 Source="{Binding ProductImage}"
然后再你通过WebService取到Product以后再把这个Binding 加上。
Something like this:
private void ResponseReady(IAsyncResult asyncResult)
{
WebRequest request = asyncResult.AsyncState as WebRequest;
WebResponse response = request.EndGetResponse(asyncResult);
using (Stream responseStream = response.GetResponseStream())
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ProductList));
ProductList productList = jsonSerializer.ReadObject(responseStream) as ProductList;
new Thread(() => {
mygrid.Dispatcher.BeginInvoke(
() =>
{
mygrid.DataContext = productList.Products[0];
Binding binding = new Binding("ProductImage");
rect.SetBinding(Image.SourceProperty, binding);
}
);
}).Start();
}
http://social.microsoft.com/Forums/zh-CN/silverlightzhchs/thread/8f1d0edc-4f5c-4ac4-8d4e-7372cef97df6