2

这张照片有什么问题?

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

这是 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");
            
        }
     
    }
4

1 回答 1

1

绑定到Image控件时,您需要绑定到BitmapSource. 这应该很简单。将属性的类型(或添加一个新的)更改为BitmapSource,然后get执行如下操作:

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

wherePathToImage是要显示的图像的可识别路径。

于 2013-06-13T13:02:41.773 回答