2014-01-28 7 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 у Перечень пунктов

я пытался что-то вроде этого, но это, кажется ее не так просто

<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

Использование наследования

Make 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; } 
} 
Смежные вопросы