在listview中绑定图像只显示字符串

时间:2013-06-13 12:50:12

标签: c# image listview binding

这张照片出了什么问题?

http://gilgameshcontrite.com/BadImage.jpg

不是显示史前植物的精美图片,而是显示位图的位置字符串!

这是XAML(摘录):

    <DataTemplate x:Key="YoungPicCell">
        <StackPanel Orientation="Horizontal">
            <Image Height="200" Width="200" Stretch="None"  Source="{Binding Path=YoungPicBmp}"    />
        </StackPanel>
    </DataTemplate>

文件名(和其他数据)在运行时从XML文件加载。

以下是在运行时从XML文件加载的数据:

    public class LVData
    {
        public string Name { get; set; }
        public string YoungPic { get; set; }
        public BitmapSource YoungPicBmp { get { return new BitmapImage(new Uri("{YoungPic}")); } }
        public string MediumPic { get; set; }
        public BitmapSource MediumPicBmp { get { return new BitmapImage(new Uri("{MediumPic}")); } }
        public string AdultPic { get; set; }
        public BitmapSource AdultPicBmp { get { return new BitmapImage(new Uri("{AdultPic}")); } }
        public bool SaltWater { get; set; }
        public bool FreshWater { get; set; }
        public bool Grasslands { get; set; }
        public bool Swamp { get; set; }
        public bool TropicalForest { get; set; }
        public bool Forest { get; set; }
        public bool ForestEdge { get; set; }
        public bool Sand { get; set; }
        public bool Coastal { get; set; }
        public bool RiverBorder { get; set; }
        public bool LakeBorder { get; set; }
        public bool Floodplain { get; set; }
    }


    public class WindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        //called when a property is changed
        protected void RaisePropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));

            }
        }

        private ObservableCollection<LVData> _plantList = new ObservableCollection<LVData>();
        public ObservableCollection<LVData> lsvData
        {
            get { return _plantList; }
            set { _plantList = value; RaisePropertyChanged("lsvData"); }
        }

        public void PopulateDataFromXML(string filename)
        {
            XDocument loaded = XDocument.Load(@"DinoIslandPlants.xml");


            var Plants = from x in loaded.Descendants("Plants")
                          select new
                          {
                              Name = x.Descendants("Name").First().Value,
                              YoungPic = x.Descendants("YoungPic").First().Value,
                              MediumPic = x.Descendants("MediumPic").First().Value,
                              AdultPic = x.Descendants("AdultPic").First().Value,
                              SaltWater = x.Descendants("SaltWater").First().Value,
                              FreshWater = x.Descendants("FreshWater").First().Value, 
                              Grasslands = x.Descendants("Grasslands").First().Value, 
                              Swamp = x.Descendants("Swamp").First().Value, 
                              TropicalForest = x.Descendants("TropicalForest").First().Value, 
                              Forest = x.Descendants("Forest").First().Value, 
                              ForestEdge = x.Descendants("ForestEdge").First().Value, 
                              Sand = x.Descendants("Sand").First().Value, 
                              Coastal = x.Descendants("Coastal").First().Value,
                              RiverBorder = x.Descendants("RiverBorder").First().Value,
                              LakeBorder = x.Descendants("LakeBorder").First().Value,
                              Floodplain = x.Descendants("Floodplain").First().Value
                          };
            foreach (var _plant in Plants)
            {
                _plantList.Add(new LVData { 
                    Name = _plant.Name,
                    YoungPic = _plant.YoungPic, 
                    MediumPic = _plant.MediumPic, 
                    AdultPic = _plant.AdultPic, 
                    SaltWater = Convert.ToBoolean(_plant.SaltWater),
                    FreshWater = Convert.ToBoolean(_plant.FreshWater),
                    Grasslands = Convert.ToBoolean(_plant.Grasslands),
                    Swamp = Convert.ToBoolean(_plant.Swamp),
                    TropicalForest = Convert.ToBoolean(_plant.TropicalForest),
                    Forest = Convert.ToBoolean(_plant.Forest),
                    Sand = Convert.ToBoolean(_plant.Sand),
                    Coastal = Convert.ToBoolean(_plant.Coastal),
                    RiverBorder = Convert.ToBoolean(_plant.RiverBorder),
                    LakeBorder = Convert.ToBoolean(_plant.LakeBorder),
                    Floodplain = Convert.ToBoolean(_plant.Floodplain) 

                });

            }

            RaisePropertyChanged("lsvData");

        }

    }

1 个答案:

答案 0 :(得分:1)

绑定到Image控件时,需要绑定到BitmapSource。这应该是非常直接的。将属性的类型(或添加新的)更改为BitmapSource,然后在get中执行以下操作:

... get { return new BitmapImage(new Uri("{PathToImage}")); }

其中PathToImage是您想要显示的图像的可识别路径。