我试图根据SelectedDay的Date属性的值导航到ListCollectionView中的特定项目。
VM
private Day _selectedDay;
public Day SelectedDay // the Name property
{
get { return _selectedDay; }
set { _selectedDay = value; RaisePropertyChanged(); }
}
public ObservableCollection<ShootingDay> AllShootingDayInfo {get; set;}
private ListCollectionView _shootingDayInfoList;
public ListCollectionView ShootingDayInfoList
{
get
{
if (_shootingDayInfoList == null)
{
_shootingDayInfoList = new ListCollectionView(AllShootingDayInfo);}
return _shootingDayInfoList;
}
set
{
_shootingDayInfoList = value; RaisePropertyChanged();
}
}
<Day>
对象的属性为Date
,我希望它与Date
对象中的<ShootingDay>
属性匹配,以便我可以导航到ShootingDayInfoList
其中SelectedDay.Date
与Date
内的ShootingDayInfoList
项匹配。
我已尝试过此功能,但由于所选项目不属于同一个对象,因此无法正常工作。
ShootingDayInfoList.MoveCurrentTo(SelectedDay.Date);
我该如何使这项工作?我对这一切都很陌生。
答案 0 :(得分:1)
您需要Filter
谓词来获取所需的项目,然后移除Filter
以恢复所有项目。
代码
ViewModel vm = new ViewModel();
System.Diagnostics.Debug.WriteLine(vm.ShootingDayInfoList.Count.ToString());
vm.SelectedDay.Date = DateTime.Parse("12/25/2015");
vm.ShootingDayInfoList.Filter = (o) =>
{
if (((ShootingDay)o).Date.Equals(vm.SelectedDay.Date))
return true;
return false;
};
ShootingDay foundItem = (ShootingDay)vm.ShootingDayInfoList.GetItemAt(0);
vm.ShootingDayInfoList.Filter = (o) => { return true; };
vm.ShootingDayInfoList.MoveCurrentTo(foundItem);
我已使用MoveCurrentToNext() method
检查了代码,但它运行正常。
这种方法不会影响您现有的代码。
第二种方法,直接使用AllShootingDayInfo
或使用SourceCollection
属性获取基础Collection
:
ViewModel vm = new ViewModel();
System.Diagnostics.Debug.WriteLine(vm.ShootingDayInfoList.Count.ToString());
vm.SelectedDay.Date = DateTime.Parse("12/23/2015");
IEnumerable<ShootingDay> underlyingCollection = ((IEnumerable<ShootingDay>)vm.ShootingDayInfoList.SourceCollection);
ShootingDay d1 = underlyingCollection.FirstOrDefault(dt => dt.Date.Equals(vm.SelectedDay.Date));
vm.ShootingDayInfoList.MoveCurrentTo(d1);