gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:52 25 4
gpt4 key购买 nike

这张图有什么问题吗?

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

这是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com