我试图显示有关汽车列表的信息。该列表存储在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));
}
}
}
}
答案 0 :(得分:0)
将CarLibrary
定义为ObservableCollection
的{{1}}是不可能的:
Car
然后使用public ObservableCollection<Car> CarLibrary {get; set;}
方法在构造函数中加载集合。假设CarLib.ReadList()
返回ReadList
,您可以使用IEnumerable<Car>
扩展方法将其转换为ObservableCollection
:
ToObservalbe
您的XAML视图对我来说很不错,应该会显示您的汽车列表。
答案 1 :(得分:0)
问题是在ListView中我设置了 ItemsSource =&#34; {Binding CarLibrary}&#34; 应该是: ItemsSource =&#34; {绑定CarLibrary.List}&#34; 。谢谢大家的帮助。