【问题标题】:Xaml Binding to objectXaml 绑定到对象
【发布时间】:2014-01-28 11:33:33
【问题描述】:

我有以下课程

    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);
        }

    }

所以我的问题是我如何对可以从我的项目列表中获取图像内部路径的元素进行绑定

我尝试过类似的方法,但似乎没那么简单

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

【问题讨论】:

    标签: c# xaml data-binding windows-phone-8 windows-phone


    【解决方案1】:

    试试这个。使用 BitmapImage 绑定图片的 Source。这个对我有用。只需更改代码中的一些内容即可。

    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"/>
    

    【讨论】:

      【解决方案2】:

      使用继承

      将 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; }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-06
        • 2011-02-28
        • 1970-01-01
        • 2015-09-11
        • 1970-01-01
        • 2013-04-15
        • 2013-05-03
        • 2015-03-25
        • 1970-01-01
        相关资源
        最近更新 更多