2014-01-28 116 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; } 
}