在listview中绑定图像只是显示路径字符串 [英] Binding an image in a listview is just displaying the path string

查看:55
本文介绍了在listview中绑定图像只是显示路径字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这张照片出了什么问题?



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



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



这里是XAML(片段):



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

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







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



这是相关的代码:






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

Here is the relevant code:

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

}

推荐答案

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)
        {
                    }
    }











在ur mainclass具有






create this class under the same namespace as ur mainclass have


这篇关于在listview中绑定图像只是显示路径字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆