Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Binding an image to a listview in C# is just displaying the path, not the image

$
0
0

What is wrong with this picture?

This is a screen capture of a detail of a listview. See where it says AlethopterisYoung.bmp? That's supposed to be a picture of a prehistoric plant. Instead, all I'm seeing is the path (for brevity I've truncated the whole string).

Here's the XAML (Snippet):

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

And here's the C# code that loads the XML file with the data and (supposedly) binds the image:

       public WindowViewModel _wvm;

        public PlantDisplay()
        {
            InitializeComponent();
            _wvm = new WindowViewModel();
            this.DataContext = _wvm;
        }

        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(filename);

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

        }

Can anybody find my mistake?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>