Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What's wrong with this picture?

Instead of displaying a nice picture of a prehistoric plant, the string of the location of the bitmap is being displayed!

Here's the XAML (snippet):

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

The filenames (and other data) are loaded at runtime from an XML file.

Here is the data being loaded from the XML file at runtime:

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

        }

    }
share|improve this question
2  
What's the type of YoungPic? –  Michael Perrenoud Jun 13 '13 at 12:54
    
It's a string; just added code above. –  zetar Jun 13 '13 at 12:57
    
Here's a link to the entire listview that shows complete path strings, etc.: dowce.com/~J8p –  zetar Jun 13 '13 at 12:58

1 Answer 1

When binding to an Image control you need to bind to a BitmapSource. This should be pretty straight forward. Change the type of the property (or add a new one) to BitmapSource and then in the get do something like this:

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

where PathToImage is a recognizable path to the image you want to display.

share|improve this answer
    
First, thanks very much... I never ever heard of a BitmapSource! Anyway, it's throwing this error when I modify the above class: must declare a body because it is not marked abstract, extern, or partial (it's complaining about the 'set') –  zetar Jun 13 '13 at 13:06
1  
@zetar: oh, since the get now has a body, you need to actually do something with the set. I'm guessing you probably need to declare a private field or get rid of it all together. –  Michael Perrenoud Jun 13 '13 at 13:30
    
If I eliminate set it throws an error (YoungPic cannot be assigned to -- it is read only... here: YoungPic = _plant.YoungPic, –  zetar Jun 13 '13 at 13:40
1  
@zetar, I think you're going to have to create a new property (like I suggested might have to be done). Put the old property back to get; set; and then build a new property that's just a get and returns the BitmapSource. When sending in the path to the Uri use the current property YoungPic to get that value. –  Michael Perrenoud Jun 13 '13 at 13:42
    
I've updated the XAML and code (above) to reflect these changes... still, unfortunately, just displaying the paths. What am I doing wrong? –  zetar Jun 13 '13 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.