2014-01-28 114 views
0

我有follwing類XAML綁定到對象

public class Noticia 
{ 
    public string texto { get; set; } 
    public string titulo { get; set; } 
    public int id { get; set; } 
    public Imagem img { get; set; } 

} 

包含該類型IMAGEM的屬性

public class Imagem 
{ 
    public string path { get; set; } 
    public string page { get; set; } 
    public int id { get; set; } 

} 

我再有另一個類創建一流

的項目清單
public class NoticiasDB 
    { 
     public List<Noticia> listaNoticias { get; set; } 

     public NoticiasDB() 
     { 
      Noticia not1 = new Noticia() { titulo = "Noticia 1", texto = "lalala", id = 1 }; 
      Noticia not2 = new Noticia() { titulo = "Noticia 1", texto = "lalala.", id = 2 }; 
      Noticia not3 = new Noticia() { titulo = "Noticia 1", texto = "lalala.", id = 3 }; 
      Noticia not4 = new Noticia() { titulo = "Noticia 1", texto = "lalala", id = 4 }; 
      Noticia not5 = new Noticia() { titulo = "Noticia 1", texto = "lalala.", id = 5 }; 
      not1.img = new Imagem() { path = "Assets/pipa.png", page = "Noticias", id = 1 }; 
      not2.img = new Imagem() { path = "Assets/midia.png", page = "Noticias", id = 2 }; 
      not3.img = new Imagem() { path = "Assets/pipa.png", page = "Noticias", id = 3 }; 
      not4.img = new Imagem() { path = "Assets/midia.png", page = "Noticias", id = 4 }; 
      not5.img = new Imagem() { path = "Assets/pipa.png", page = "Noticias", id = 5 }; 


      listaNoticias = new List<Noticia>(); 

      listaNoticias.Add(not1); 
      listaNoticias.Add(not2); 
      listaNoticias.Add(not3); 
      listaNoticias.Add(not4); 
      listaNoticias.Add(not5); 
     } 

    } 

所以我的問題是我如何做一個元素的綁定,可以從m中獲取圖像中的路徑項目

Y的名單我想這樣的事情,但

<Image Name="NewsImg" Source="{Binding img.path}" Stretch="Fill" Grid.Column="0"/> 

回答

1

試試這個看來它不是那麼簡單。使用BitmapImage綁定圖像源。這個對我有用。只需更改代碼中的幾件事情即可。

public class Imagem 
{ 
    public string path { get; set; } 
    public string page { get; set; } 
    public int id { get; set; } 
    public BitmapImage ImageSource{get;set;} 
} 

not1.img = new Imagem() { path = "Assets/pipa.png", page = "Noticias", id = 1 , ImageSource = new BitmapImage(new Uri("Assets/pipa.png",UriKind.Relative)) }; 

<Image Name="NewsImg" Source="{Binding img.ImageSource}" Stretch="Fill" Grid.Column="0"/> 
0

使用繼承

製作IMAGEM作爲基類和其子

這樣

public class Noticia : Imagem 
{ 
    public string texto { get; set; } 
    public string titulo { get; set; } 
    public int id { get; set; } 
    public Imagem img { get; set; } 
}