2014-01-07 98 views
0

我使用LazyListBox,如下所示。數據模板中有一個圖像,而且我無法綁定ImageUrl.How可以綁定ImageSource。在Windows Phone中綁定ImageSource

<lazy:LazyListBox x:Name="d" ItemSource={Binding ProductImageLIst}> 
      <lazy:LazyListBox.LoadedItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Product_Id}" Foreground="Black"></TextBlock> 
         <Image x:Name="img" Source="{Binding Path=ImageUrl}"></Image> 
        </StackPanel> 
       </DataTemplate> 
      </lazy:LazyListBox.LoadedItemTemplate> 
     </lazy:LazyListBox> 

而且我ProductImageList類如下所示

public class ProductImageList 
    { 

     public string ImageId { get; set; } 
     public string ImageUrl{ get; set; } 
     public string Product_Id { get; set; } 
     public string Category_id { get; set; } 
     public ProductImageList() 
     { 

     } 
     public ProductImageList(string imageid, string imageurl, string productid,string catid) 
     { 
      this.ImageId = imageid; 
      this.ImageUrl = imageurl; 
      this.Product_Id = productid; 
      this.Category_id = catid; 

     } 
    } 

回答

2

使用的BitmapImage在LazyListBox控件綁定圖像的源。這是解決方案。如果你的'ImageUrl'是一個http url,你應該首先從這個URL下載圖像,並通過下載的圖像流創建BitmapImage,如果你的imageUrl是一個相對URL創建BitmapImage如下。

<lazy:LazyListBox x:Name="d" ItemSource={Binding ProductImageLIst}> 
      <lazy:LazyListBox.LoadedItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Product_Id}" Foreground="Black"></TextBlock> 
         <Image x:Name="img" Source="{Binding Path=ImageSource}"></Image> 
        </StackPanel> 
       </DataTemplate> 
      </lazy:LazyListBox.LoadedItemTemplate> 
     </lazy:LazyListBox> 

public class ProductImageList 
    { 

     public string ImageId { get; set; } 
     public string ImageUrl{ get; set; } 
     public string Product_Id { get; set; } 
     public string Category_id { get; set; } 
     public BitmapImage ImageSource{get;set;} 
     public ProductImageList() 
     { 

     } 
     public ProductImageList(string imageid, string imageurl, string productid,string catid) 
     { 
      this.ImageId = imageid; 
      this.ImageUrl = imageurl; 
      this.Product_Id = productid; 
      this.Category_id = catid; 
      this.ImageSource = new BitmapImage(new Uri(imageurl, UriKind.RelativeOrAbsolute)); 

     } 
    }