c# - 在 ListView 中绑定(bind)图像只显示字符串

标签 c# image listview binding

这张图有什么问题吗?

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

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

最佳答案

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

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

其中 PathToImage 是您要显示的图像的可识别路径。

关于c# - 在 ListView 中绑定(bind)图像只显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087523/

相关文章:

Java将CMYK图像保存到文件

android - 以与用户手指在布局上移动相同的速度更改相对布局的高度

wpf - 如何使用 TwoWay 模式将 Listview SelectedItem 绑定(bind)到文本框?

c# - 方法未找到 : void Amazon. S3.AmazonS3Client..ctor() |带有 .NET/Xamarin 的 AmazonS3

ios - 在 Collection View 单元格中使用淡入淡出效果动画图像

c# - SQL Server Azure - 每 x 分钟执行一次存储过程

html - 将图像粘贴到 div 的中心

java - Android - 使用 Volley 显示自定义 ListView (使用 http 请求发送 header )

c# - XNA GS 安装程序无法识别 c#?

c# - 在 C# 和 C++ 中声明数组有什么区别?