Click here to Skip to main content
15,896,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What's wrong with this picture?

http://gilgameshcontrite.com/BadImage.jpg[^]

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):

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



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

Here is the relevant code:

C#
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");
      }
}
Posted
Updated 13-Jun-13 7:07am
v2

1 solution

You can use converter for the same .Please have a look of following

<Window
xmlns:local1="clr-namespace:**********">
</window>


<Window.Resources>
<local1:UriToBitmapConverter x:Key="Imageconverter"></local1:UriToBitmapConverter>
</Window.Resources>

<Image Height="200" Width="200" Stretch="None"  Source="{Binding FrameBrdr, Converter={StaticResource Imageconverter}}"  />


need to create a converter with following code :- 

public class UriToBitmapConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                BitmapImage bi = new BitmapImage();
                if (value != null)
                {
                    using (FileStream fileStream = File.OpenRead(value.ToString()))
                    {

                        MemoryStream ms = new MemoryStream();
                        fileStream.CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        fileStream.Close();
                        bi.BeginInit();
                        bi.CacheOption = BitmapCacheOption.OnLoad;
                        bi.StreamSource = ms;
                        bi.EndInit();
                        bi.Freeze();

                    }
                }
                else
                {
                    bi = new BitmapImage();
                }

                return bi;
            }
            catch (Exception ex)
            {
               return null;
            }
            finally
            {
                GC.AddMemoryPressure(10000);
                GC.Collect();
            }

}
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
                    }
    }





create this class under the same namespace as ur mainclass have
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900