【发布时间】:2014-10-06 09:19:34
【问题描述】:
我创建了一个模型类“RestaurentList”,它使用来自 json 文件的数据填充两个集合。 在我的 ViewModel 中将对象实例化为类后,我将集合数据绑定到 ItemsControl。
上面的一切都很好,但是当我从我的 ViewModel 中的对象调用方法 populatePartialList 时,它不包含我实例化对象时的任何数据。 这意味着,当我的方法尝试重新填充 PartialList 时,它不能,因为它没有从 FullList 中找到数据。
编辑:我遗漏了一些代码,正如您在注释标签中看到的那样。 我只是想让你了解我是如何做到这一点的。
我的问题基本上是,为什么当我调用方法 populatePartialList 时对象不包含任何数据。
我猜这与我将 List 数据绑定到 ItemsControl 的事实有关,因此无法再访问它?那这种情况应该怎么办呢?我正在尝试制作一个非常简单的分页
对以上内容进行编辑;我尝试删除我的绑定,但仍然无法访问数据。
型号:
public class RestaurentList
{
private ObservableCollection<Restaurent> _fullList = new ObservableCollection<Restaurent>();
private ObservableCollection<Restaurent> _partialList = new ObservableCollection<Restaurent>();
public ObservableCollection<Restaurent> FullList
{
get { return _fullList; }
}
public ObservableCollection<Restaurent> PartialList
{
get { return _partialList; }
}
public RestaurentList()
{
populateList();
}
public void populatePartialList(int fromValue = 1)
{
int collectionAmount = _fullList.Count;
int itemsToShow = 2;
fromValue = (fromValue > collectionAmount ? 1 : fromValue);
foreach (Restaurent currentRestaurent in _fullList)
{
int currentId = Convert.ToInt32(currentRestaurent.UniqueId);
if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1))
{
_partialList.Add(currentRestaurent);
}
}
}
private async void populateList()
{
// Get json data
foreach (JsonValue restaurentValue in jsonArray)
{
// populate full list
foreach (JsonValue menuValue in restaurentObject["Menu"].GetArray())
{
// populate full list
}
this._fullList.Add(restaurent);
}
populatePartialList();
}
public override string ToString()
{
// Code
}
}
查看模型:
class ViewModelDefault : INotifyPropertyChanged
{
private RestaurentList _list;
public ObservableCollection<Restaurent> List
{
get { return _list.PartialList; }
}
public ViewModelDefault()
{
_list = new RestaurentList();
_list.populatePartialList(2); // This is where i don't see the data from RestaurentList
}
#region Notify
}
为乔恩编辑:
public RestaurentList()
{
PopulatePartialList();
}
public async void PopulatePartialList(int fromValue = 1)
{
await PopulateList();
int collectionAmount = _fullList.Count;
int itemsToShow = 2;
fromValue = (fromValue > collectionAmount ? 1 : fromValue);
foreach (Restaurent currentRestaurent in _fullList)
{
int currentId = Convert.ToInt32(currentRestaurent.UniqueId);
if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1))
{
_partialList.Add(currentRestaurent);
}
}
}
private async Task PopulateList()
{
}
【问题讨论】:
-
附带说明,餐厅以
ant结尾,而不是ent- 现在是修改名称的好时机。我还强烈建议您遵循方法的 .NET 命名约定,即PopulatePartialList等。
标签: c# .net xaml data-binding observablecollection