列出数据绑定
本文关键字:数据绑定 | 更新日期: 2023-09-27 17:55:36
我试图显示有关汽车列表的信息。该列表存储在CarLibrary中,我无法获取要在相应标题下方显示的信息。
这是我的 xaml:
<TabItem Header="Overview">
<Grid>
<ListView Margin="10" ItemsSource="{Binding CarLibrary}">
<ListView.View>
<GridView>
<GridViewColumn Header="Picture" Width="100" DisplayMemberBinding="{Binding PicturePath}"/>
<GridViewColumn Header="Year" Width="100" DisplayMemberBinding="{Binding Year}"/>
<GridViewColumn Header="Make" Width="100" DisplayMemberBinding="{Binding Make}"/>
<GridViewColumn Header="Model" Width="100" DisplayMemberBinding="{Binding Model}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</TabItem>
以下是 c# 中视图模型的相关代码,每个 Car 对象都具有 Make、Model、Year 和 PicturePath 的属性:
namespace CarApp
{
public class Carvm : INotifyPropertyChanged
{
private CarLib carLibrary;
public CarLib CarLibrary
{
get { return carLibrary; }
set
{
carLibrary = value;
NotifyPropertyChanged("CarLibrary");
}
}
public Carvm()
{
carLibrary = new CarLib();
newCar = new Car();
currentCar = new Car();
rmCommand = new DeleteCommand(carLibrary);
touchCommand = new AddCommand(carLibrary, newCar);
//load list from disk on startup
carLibrary.ReadList();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
这是CarLib:
namespace CarApp
{
public class CarLib : INotifyPropertyChanged
{
private ObservableCollection<Car> list;
public ObservableCollection<Car> List
{
get { return list; }
set {
list = value;
NotifyPropertyChanged("List");
}
}
public CarLib()
{
List = new ObservableCollection<Car>();
}
/// <summary>
/// Deserializes list of cars and returns the list to user
/// </summary>
public void ReadList()
{
//create local list to hold data
ObservableCollection<Car> carList = new ObservableCollection<Car>();
try
{
using (Stream stream = File.Open("data.bin", FileMode.OpenOrCreate))
{
BinaryFormatter bin = new BinaryFormatter();
try
{
//point List to deserialized stream
List = (ObservableCollection<Car>)bin.Deserialize(stream);
}
catch (SerializationException)
{
Console.WriteLine("The list is empty.");
}
}
}
catch (SerializationException e)
{
if (e.Source != null)
{
Console.WriteLine("Empty List");
}
}
}
/// <summary>
/// Serialize list supplied by the user and write to disk
/// </summary>
public void WriteList()
{
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
//write list to disk
bin.Serialize(stream, List);
}
}
catch (IOException)
{
Console.WriteLine("Empty List");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
问题是在ListView中,我设置了ItemsSource="{Binding CarLibrary}",而它应该是:ItemsSource="{Binding CarLibrary.List}"。谢谢大家的帮助。
将
CarLibrary
定义为Car
的ObservableCollection
是很好的:
public ObservableCollection<Car> CarLibrary {get; set;}
然后使用 CarLib.ReadList()
方法在构造函数中加载集合。假设ReadList
返回IEnumerable<Car>
然后您可以使用扩展方法将其转换为ObservableCollection
ToObservalbe
:
CarLibrary = new CarLib().ReadList().ToObservable();
你的 XAML 视图对我来说看起来不错,这应该呈现你的汽车列表。